AWS Java SDK:检查S3存储桶中是否存在具有特定版本的对象

时间:2022-09-25 18:43:38

I know there is a doesObjectExist method to check if an object exists in a specified bucket, but how do I check if an object with a specific version exists in an S3 bucket?

我知道有一个doesObjectExist方法来检查指定存储桶中是否存在对象,但是如何检查S3存储桶中是否存在具有特定版本的对象?

I want to call doesObjectExist(bucketName, objectName, s3Version).

我想调用doesObjectExist(bucketName,objectName,s3Version)。

Is there any way I can do this, or do I need to call listVersions first and check if the version exists using the VersionListing? This approach seems a lot more verbose.

有什么方法可以做到这一点,或者我是否需要先调用listVersions并使用VersionListing检查版本是否存在?这种方法似乎更冗长。

1 个解决方案

#1


1  

There is no one step check in the current API. You could try using something like

当前的API中没有一步检查。你可以尝试使用类似的东西

s3Client.getObjectMetadata(
  new GetObjectMetadataRequest(bucketName, key, versionId)
)

But then I don't see any reliable way to know when such object doesn't exist (because there's no a special "object doesn't exist" exception for such case). So after it fails you should check that this object exists with doesObjectExist. Or another way round: check that it exists, then query the metadata with version, if it exists but the metadata request fails, this version of the object doesn't exist.

但是后来我没有看到任何可靠的方法来知道这个对象何时不存在(因为这种情况下没有特殊的“对象不存在”异常)。因此,在失败之后,您应该检查此对象是否存在于didObjectExist中。或者另一种方式:检查它是否存在,然后用版本查询元数据,如果它存在但元数据请求失败,则该对象的版本不存在。

#1


1  

There is no one step check in the current API. You could try using something like

当前的API中没有一步检查。你可以尝试使用类似的东西

s3Client.getObjectMetadata(
  new GetObjectMetadataRequest(bucketName, key, versionId)
)

But then I don't see any reliable way to know when such object doesn't exist (because there's no a special "object doesn't exist" exception for such case). So after it fails you should check that this object exists with doesObjectExist. Or another way round: check that it exists, then query the metadata with version, if it exists but the metadata request fails, this version of the object doesn't exist.

但是后来我没有看到任何可靠的方法来知道这个对象何时不存在(因为这种情况下没有特殊的“对象不存在”异常)。因此,在失败之后,您应该检查此对象是否存在于didObjectExist中。或者另一种方式:检查它是否存在,然后用版本查询元数据,如果它存在但元数据请求失败,则该对象的版本不存在。