如何在Google云端存储中使用bucket.upload()而不是file.createWriteStream()?

问题描述 投票:0回答:2

我正在尝试将文件上传到Google云端存储后获取永久(未签名)下载网址。我可以使用file.createWriteStream()获取签名的下载URL,但file.createWriteStream()不会返回包含未签名下载URL的UploadResponsebucket.upload()包括UploadResponseGet Download URL from file uploaded with Cloud Functions for Firebase有几个答案解释如何从UploadResponse获取未签名的下载URL。如何将代码中的file.createWriteStream()更改为bucket.upload()?这是我的代码:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({ projectId: 'my-app' });
const bucket = storage.bucket('my-app.appspot.com');
var file = bucket.file('Audio/' + longLanguage + '/' + pronunciation + '/' + wordFileType);

const config = {
  action: 'read',
  expires: '03-17-2025',
  content_type: 'audio/mp3'
};

function oedPromise() {
  return new Promise(function(resolve, reject) {
    http.get(oedAudioURL, function(response) {
        response.pipe(file.createWriteStream(options))
        .on('error', function(error) {
          console.error(error);
          reject(error);
        })
        .on('finish', function() {
          file.getSignedUrl(config, function(err, url) {
            if (err) {
              console.error(err);
              return;
            } else {
              resolve(url);
            }
          });
        });
      });
    });
  }

我试过这个,它不起作用:

  function oedPromise() {
    return new Promise(function(resolve, reject) {
      http.get(oedAudioURL, function(response) {
        bucket.upload(response, options)
        .then(function(uploadResponse) {
          console.log('Then do something with UploadResponse.');
        })
        .catch(error => console.error(error));
      });
    });
  }

错误消息是Path must be a string.换句话说,response是一个变量但需要是一个字符串。

node.js google-cloud-platform google-cloud-storage
2个回答
0
投票

bucket.upload()是围绕file.createWriteStream()的便捷包装器,它接受本地文件系统路径并将文件作为对象上传到存储桶中:

bucket.upload("path/to/local/file.ext", options)
  .then(() => {
    // upload has completed
  });

要生成签名URL,您需要从存储桶中获取文件对象:

const theFile = bucket.file('file_name');

文件名将是本地文件的名称,或者如果您为GCS上的文件指定了备用远程名称options.destination

然后,使用File.getSignedUrl()获取签名的URL:

bucket.upload("path/to/local/file.ext", options)
  .then(() => {
    const theFile = bucket.file('file.ext');
    return theFile.getSignedURL(signedUrlOptions); // getSignedURL returns a Promise
  })
  .then((signedUrl) => {
    // do something with the signedURL
  });

看到:

Bucket.upload() documentation

File.getSignedUrl() documentation


0
投票

您可以使用makePublic方法将存储桶中的特定文件公开读取。

来自文档:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

// 'my-bucket' is your bucket's name
const myBucket = storage.bucket('my-bucket');

// 'my-file' is the path to your file inside your bucket
const file = myBucket.file('my-file');

file.makePublic(function(err, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.makePublic().then(function(data) {
  const apiResponse = data[0];
});

现在,URI http://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]是该文件的公共链接,如here所述。

关键是你只需要这个最小的代码来公开一个对象,例如使用Cloud Function。然后,您已经知道了公共链接的方式,可以直接在您的应用中使用它。

© www.soinside.com 2019 - 2024. All rights reserved.