how can I download image on firebase storage?

后端 未结 5 535
难免孤独
难免孤独 2021-01-06 14:53

I want to download image on firebase storage in android app.

this is my image

I try this but it is not working

storageR         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 15:26

    I'm a little late on this question. I encountered this same problem, but getting the URL (ie: getDownloadUrl() as shown in the Firebase docs) for the file doesn't address the entire problem which is downloading the file.

    This is because sending a download (or attachment) response to the browser requires the file content (not the URL). In order to get the URL and obtain the actual file content requires an HTTP request which isn't an option for us using the free Spark Firebase account.

    To get around this, I used the download() method from the storage bucket which allowed me save the storage file as a local tmp file. Then I used an fs read stream to pipe the file content to the cloud function response...

    const fireapp = '.appspot.com';
    const gcs = require('@google-cloud/storage')({keyFilename:'serviceAccount.json'});
    const bucket = gcs.bucket(fireapp);
    
    // storage path
    let fn = 'folder/theimage.png';
    
    // allow CORS
    res.set('Access-Control-Allow-Origin', '*');
    
    // download w/o external http request 
    // by download file from bucket
    // and saving to tmp folder
    const tempFilePath = path.join(os.tmpdir(), "tmpfile.jpg");
    bucket.file(fn).download({destination: tempFilePath}).then(()=>{
        console.log('Image downloaded locally to', tempFilePath);
        var filestream = fs.createReadStream(tempFilePath);
        filestream.pipe(response);
    });
    

提交回复
热议问题