java - 使用 S3Client 在 S3 上重命名文件名
问题描述
我尝试重命名 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);
}
解决方案
我找到了解决方案。在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);}
推荐阅读
- c - C代码每次都给出零值。我能纠正什么?
- php - 如何在 laravel 中只加入表的最后一条记录?
- pandas - 如何最终确定 value_counts
- vim - g++致命错误没有这样的文件或目录
- quill - Quill中的方程框不完全可见
- c++ - 多个线程可以同时在同一个接口上嗅探吗?
- symfony - Symfony,来自约束的 minMessage 未显示
- python - 使用 BeautifulSoup python 访问站点时访问被拒绝 [403]
- azure-web-app-service - Azure 应用程序网关支持 Open ID 连接身份验证
- sql - 窗口函数在逻辑标准上增加列