检查将对象上传到S3后是否存在。

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

check existance of object after uploading it to S3

问题

我想要检查一个对象是否在成功上传到S3后立刻上传。

PutObjectRequest putObjReq = new PutObjectRequest(bucketName, s3Key, tmpArchivePath.toFile());
Upload upload = transferManager.upload(putObjReq);
upload.waitForCompletion();
boolean fileExistsAfter = s3Client.doesObjectExist(bucketName, s3Key);

这段代码总是返回true,即使对象没有成功上传。我认为这是因为事件ual Consistency,例如:当s3Key以/开头时,它不是一个有效的键,所以对象不会被上传,但它仍然返回true。

英文:

I want to check if an object us successfuly uploaded to S3 just after uploading it.

PutObjectRequest putObjReq = new PutObjectRequest(bucketName, s3Key, tmpArchivePath.toFile());
Upload upload = transferManager.upload(putObjReq);
upload.waitForCompletion();
boolean fileExistsAfter = s3Client.doesObjectExist(bucketName, s3Key);

that returns always true ,even if the object is not successfully uploaded. I think that is because of the Eventual Consistency,
example:
when the s3Key starts with / , it is not a valid key ,so the object will not be uploaded however it returns true

答案1

得分: 1

Your code is working correctly.

When you upload to a key such as /dog.png, you do indeed get an S3 object with the key /dog.png. You can see this if you run aws s3 ls s3://mybucket// (with extra trailing slash) instead of aws s3 ls s3://mybucket/. You can also verify this by issuing a HeadObject request for the key /dog.png, which is effectively what the doesObjectExist() method does.

To verify the object exists using the awscli, run:

aws s3api head-object --bucket mybucket --key /dog.png

So the upload succeeds and doesObjectExist() correctly returns true.

As a general rule, you probably want to avoid leading slash because it is confusing for people.

英文:

Your code is working correctly.

When you upload to a key such as /dog.png, you do indeed get an S3 object with the key /dog.png. You can see this if you run aws s3 ls s3://mybucket// (with extra trailing slash) instead of aws s3 ls s3://mybucket/. You can also verify this by issuing a HeadObject request for the key /dog.png, which is effectively what the doesObjectExist() method does.

To verify the object exists using the awscli, run:

aws s3api head-object --bucket mybucket --key /dog.png

So the upload succeeds and doesObjectExist() correctly returns true.

As a general rule, you probably want to avoid leading slash because it is confusing for people.

huangapple
  • 本文由 发表于 2023年7月20日 17:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728465.html
匿名

发表评论

匿名网友

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

确定