英文:
Rename filename on S3 using S3Client
问题
我尝试对AWS驱动器上的文件名进行重命名。我与AWS的连接已配置好。我准备了一段代码,应该会复制具有更改后文件名的新文件,但实际上它只是复制了文件,并未改变文件名。
void updateFileName(String key, String newKey, String newName) {
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(bucket + "/" + key)
.destinationBucket(bucket)
.destinationKey(newKey)
.contentDisposition("attachment; filename=" + newName)
.build();
s3Client.copyObject(copyReq);
}
英文:
I try to rename filename on AWS drive. My connection with AWS is configured. I prepared a piece of code that should copy new file with changed filename, but it copies file with unchanged filename.
void updateFileName(String key, String newKey, String newName) {
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(bucket + "/" + key)
.destinationBucket(bucket)
.destinationKey(newKey)
.contentDisposition("attachment; filename=" + newName)
.build();
s3Client.copyObject(copyReq);
}
答案1
得分: 3
我找到了解决方案。在 CopyObjectRequest
的构建器中,我添加了 .metadataDirective(MetadataDirective.REPLACE)
。代码应该如下所示:
void updateFileName(String key, String newKey, String newName) {
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(bucket + "/" + key)
.destinationBucket(bucket)
.destinationKey(newKey)
.contentDisposition("attachment; filename=" + newName)
.metadataDirective(MetadataDirective.REPLACE)
.build();
s3Client.copyObject(copyReq);
}
英文:
I found the solution. In the builder of CopyObjectRequest
I added .metadataDirective(MetadataDirective.REPLACE)
. The code should look like:
void updateFileName(String key, String newKey, String newName) {
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(bucket + "/" + key)
.destinationBucket(bucket)
.destinationKey(newKey)
.contentDisposition("attachment; filename=" + newName)
.metadataDirective(MetadataDirective.REPLACE)
.build();
s3Client.copyObject(copyReq);}
答案2
得分: 1
官方的 AWS 文档中有一个名为 "复制、移动或重命名对象" 的部分,你可以在这里找到。
其中提到:
> 你可以使用 AmazonS3 客户端的 copyObject
方法将对象从一个存储桶复制到另一个存储桶。需要提供源存储桶的名称、要复制的对象以及目标存储桶的名称。
对你来说,相关的代码片段如下:
s3.copyObject(from_bucket, object_key, to_bucket, object_key);
你还可以在 GitHub 上找到一个完整的示例,在这里。
需要注意的是,在 AWS S3 上,更新/重命名/移动对象的概念实际上不太适用。SDK 和其他更高级别的库可能会暴露允许执行此类操作的 API,但归根结底,无论如何,对于更新操作,你要么是在重写同一对象,对于重命名/移动操作,你要么是在写入新对象并删除旧对象。
在我上面提供的同一页文档中还有一条说明:
> 你可以结合使用 copyObject
和 deleteObject
来移动或重命名对象,首先将对象复制到新名称(可以使用同一个存储桶作为源和目标),然后从旧位置删除对象。
英文:
The official AWS Docs have a section called "Copy, Move, or Rename Objects" that you can find here.
It states:
> You can copy an object from one bucket to another by using the AmazonS3 client’s copyObject
method. It takes the name of the bucket to copy from, the object to copy, and the destination bucket name.
The relevant piece of code for you is this:
s3.copyObject(from_bucket, object_key, to_bucket, object_key);
You can also find a full example on GitHub here.
Something to note, on AWS S3 the concepts of update/rename/move an object don't really apply. SDKs and other higher level libraries might expose APIs that allow to perform such operations but at the end of the day you're either rewriting the same object in case of an update or writing a new one and deleting the old one in case of rename/move.
This is also explained in a note in the same page I linked above:
> You can use copyObject
with deleteObject
to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论