成功上传后生成下载URL [英] Generate Download URL After Successful Upload

查看:158
本文介绍了成功上传后生成下载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过JS通过Google云端存储将文件成功上传到Firebase的存储空间!我注意到的是,与直接上传的文件不同的是,通过Google Cloud上传的文件只有一个存储位置URL,它不是一个完整的URL,这意味着它不能被读取!我想知道是否有一种方法可以为Firebase实际存储的下载网址部分生成完整的上传网址。

I have successfully uploaded files to Firebase's storage via Google Cloud Storage through JS! What I noticed is that unlike files uploaded directly, the files uploaded through Google Cloud only have a Storage Location URL, which isn't a full URL, which means it cannot be read! I'm wondering if there is a way to generate a full URL on upload for the "Download URL" part of Firebase's actual storage.

正在使用的代码:

    var filename = image.substring(image.lastIndexOf("/") + 1).split("?")[0];
    var gcs = gcloud.storage();
     var bucket = gcs.bucket('bucket-name-here.appspot.com');

     request(image).pipe(bucket.file('photos/' + filename).createWriteStream(
     {metadata: {contentType: 'image/jpeg'}}))
      .on('error', function(err) {})
    .on('finish', function() {



     console.log(imagealt);    


  });


推荐答案

使用GCloud客户端时, href =https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.36.0/storage/file?method=getSignedUrl =nofollow> getSignedUrl() 下载文件,如下所示:

When using the GCloud client, you want to use getSignedUrl() to download the file, like so:

bucket.file('photos/' + filename).getSignedUrl({
  action: 'read',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to read from this URL.
  request(url, function(err, resp) {
    // resp.statusCode = 200
  });
});

这篇关于成功上传后生成下载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆