如何在写入时将文件设为Google云存储中的公开?

huangapple go评论72阅读模式
英文:

How do I make a file public in google cloud storage at the time of writing?

问题

以下是您的翻译:

我有一段在Node.js中使用firebase-admin的代码片段,有时会因为并发问题而失败。

> 对象“<路径>”的元数据在操作期间被编辑。请重试。

const file = storage.bucket().file(photoPath);
await file.save(pngData);
await file.makePublic();

我希望在运行此代码时,这些应该是新文件,所以我猜测发生了文件上传并触发了更新文件的操作。我没有触发器,所以我猜测可能是谷歌或Firebase的某些管理操作。

理想情况下,我宁愿一次性创建具有公共权限的文件,而不是进行重试。这些文件是用于移动应用程序和网站的。

我看到save有一些选项,可以允许以下操作,但两者都创建了没有公共URL的文件。

await file.save(pngData, { public: true });
// 或者
await file.save(pngData, { predefinedAcl: &#39;publicRead&#39; });

是否有人有关于如何处理这个问题的任何想法?

编辑
有时会在file.makePublic()上抛出以下错误:

对象“&lt;路径&gt;”的元数据在操作期间被编辑。请重试。
    在新的ApiError (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:79:15)
    Util.parseHttpRespBody (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:214:38)
    Util.handleResp (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:155:117)
    /Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:535:22 上的
    对回应 (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/retry-request/index.js:240:7)
    /Users/gibb-mac/Documents/Projects/abdoc/node_modules/teeny-request/src/index.ts:333:11 上的
    运行微任务 (&lt;anonymous&gt;)
    在处理进程 (node:internal/process/task_queues:96:5) 中进行了刻度和拒绝处理
英文:

I have this snippet of code using firebase-admin in nodejs that sometimes fails due to a concurrency issue.

> The metadata for object "<path>" was edited during the operation. Please try again.

const file = storage.bucket().file(photoPath);
await file.save(pngData);
await file.makePublic();

I expect these to be new files when this code is ran, so my guess on what's happening is that the file gets uploaded and a trigger happens that updates the file. I have no triggers so my guess is it might be something administrative google or firebase is doing.
Ideally, I'd rather just create the file with it public with the same command rather than make a retry.
These files are meant to be consumed on a mobile application and website.

I see that save has options that allow for the following, but both create files that don't have public urls.

await file.save(pngData, { public: true });
// or
await file.save(pngData, { predefinedAcl: &#39;publicRead&#39; });

Does anyone have any ideas on how to handle this?

Edit
The following gets thrown occasionally on file.makePublic()

The metadata for object &quot;&lt;path&gt;&quot; was edited during the operation. Please try again.
    at new ApiError (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:79:15)
    at Util.parseHttpRespBody (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:214:38)
    at Util.handleResp (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:155:117)
    at /Users/gibb-mac/Documents/Projects/abdoc/node_modules/@google-cloud/storage/build/src/nodejs-common/util.js:535:22
    at onResponse (/Users/gibb-mac/Documents/Projects/abdoc/node_modules/retry-request/index.js:240:7)
    at /Users/gibb-mac/Documents/Projects/abdoc/node_modules/teeny-request/src/index.ts:333:11
    at runMicrotasks (&lt;anonymous&gt;)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)```

</details>


# 答案1
**得分**: 1

我意识到我从中提取图像使用的数据集中有一些重复。每当我尝试在同一批次中再次将文件设为公共时,就会出现此错误。

抱歉。

<details>
<summary>英文:</summary>

I realized my data set that I derive what images to use have some duplicates. This error gets thrown whenever I try making the file public again in the same batch.

Sorry about that.

</details>



huangapple
  • 本文由 发表于 2023年1月4日 03:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/74997669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定