首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >REACT -调整Google-Cloud-Storage上的图像大小-使用存储桶时出错

REACT -调整Google-Cloud-Storage上的图像大小-使用存储桶时出错
EN

Stack Overflow用户
提问于 2020-06-07 20:12:42
回答 1查看 63关注 0票数 0

我跟随学院的youtube视频去看"Firebase Cloud Functions --上传后调整图像大小“,但我被卡住了,试图用storage.bucket(存储桶)解决一个问题。

这是我面临的错误:

代码语言:javascript
复制
1. (node:2) MetadataLookupWarning: received unexpected error = URL is not defined code = UNKNOWN
2. Error: Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.
    at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:59:15)
    at Util.parseHttpRespMessage (/srv/node_modules/@google-cloud/common/build/src/util.js:161:41)
    at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:135:76)
    at Duplexify.requestStream.on.on.res (/srv/node_modules/@google-cloud/storage/build/src/file.js:823:31)
    at emitOne (events.js:116:13)
    at Duplexify.emit (events.js:211:7)
    at emitOne (events.js:116:13)
    at DestroyableTransform.emit (events.js:211:7)
    at onResponse (/srv/node_modules/retry-request/index.js:200:19)
    at PassThrough.<anonymous> (/srv/node_modules/retry-request/index.js:152:11) 

代码:

代码语言:javascript
复制
exports.onFileChange = functions.storage.object().onFinalize((object, context) => {
  // ...
  // Extract object data - left out to focus on the other parts of the function
  const {Storage} = require('@google-cloud/storage');

  // Creates a client
  const storage = new Storage({
      projectId: "dude-garden-care"
  });
  console.log(object, context)
  const bucket = object.bucket;
  const contentType = object.contentType;
  const filePath = object.name;

  if (object.resourceState === 'not_exists') {
    console.log('We deleted a file, exit...')
    return
  }


  if (path.basename(filePath).startsWith('resized-')) {
    console.log('We already renamed that file!')
    return
  }


  const destBucket = storage.bucket(bucket)
  const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath))
  const metadata = { contentType: contentType }
  return destBucket
    .file(filePath)
    .download({
      destination: tmpFilePath,
    })
    .then(() => {
      return spawn('convert', [tmpFilePath, '-resize', '500x500', tmpFilePath])
    })
    .then(() => {
      return destBucket.upload(tmpFilePath, {
        destination: 'resized-' + path.basename(filePath),
        metadata: metadata,
      })
    })
})

非常感谢你们的帮助

EN

回答 1

Stack Overflow用户

发布于 2020-06-08 10:27:42

我用以下方法解决了这个问题

代码语言:javascript
复制
const admin = require('firebase-admin');
const storage = admin.storage();
const destBucket = storage.bucket(bucket)

希望它能帮助到一些人。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62245148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档