gpt4 book ai didi

java - blob.getContent() 中的 com.google.cloud.storage.StorageException 404

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:07 24 4
gpt4 key购买 nike

我正在运行一项在 GCP 存储桶中创建或更新对象的服务。即我的代码检查对象是否存在,如果存在我的代码读取它,更新它并写回它。有时我在尝试读取对象时会遇到异常。我的代码:

Storage storage = googleStorage.get();
BlobId blobId = BlobId.of(STORAGE_BUCKET, "path/to.obj"));
Blob blob = storage.get(blobId);

if (blob == null) return null;

byte[] blobContent = blob.getContent();
...

堆栈跟踪:

...
at com.google.cloud.storage.Blob.getContent(Blob.java:455)
at com.google.cloud.storage.StorageImpl.readAllBytes(StorageImpl.java:461)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at com.google.cloud.storage.StorageImpl$16.call(StorageImpl.java:461)
at com.google.cloud.storage.StorageImpl$16.call(StorageImpl.java:464)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.load(HttpStorageRpc.java:588)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:220)
No such object: bucket/path/to.obj
com.google.cloud.storage.StorageException: 404 Not Found

如果对象不存在,我希望在 blob 中得到 null,或者如果 blob 不为 null,我希望能够读取。

此行为导致对象被更新多次(不确定这是因为我的代码重试调用还是因为存储库正在做的事情)。

我使用的是 google-cloud-storage 1.27.0,大约每 ~10K 个对象发生一次。

最佳答案

我已经测试了您提供的代码,它似乎以预期的方式工作 - 如果无法找到云存储对象或路径不正确,Blob 对象将被分配为 null。

失败的发生率非常低。也许如果您使用 RetryParams 配置指数退避类,则可以消除或减少这些故障的影响。

关于java - blob.getContent() 中的 com.google.cloud.storage.StorageException 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50042801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com