如何重命名 Amazon S3 存储桶中的文件? [复制]
Posted
技术标签:
【中文标题】如何重命名 Amazon S3 存储桶中的文件? [复制]【英文标题】:How to rename a file in Amazon S3 Bucket? [duplicate] 【发布时间】:2014-06-15 14:21:45 【问题描述】:我正在尝试重命名 S3 存储桶中的文件。这是一个代码sn-p
S3Object s3Obj = getS3Client().getObject(new GetObjectRequest(getBucketName(), fileName));
//Error in Above Line itself
getS3Client().putObject(getBucketName(), newFileName, s3Obj.getObjectContent(), s3Obj.getObjectMetadata());
private AmazonS3 getS3Client()
AWSCredentials myCredentials = new BasicAWSCredentials(AccessKey,SecretKey);
AmazonS3 s3client = new AmazonS3Client(myCredentials);
return s3client;
所以,我收到了这个错误,
DEBUG [main] request.handleErrorResponse(748) |收到错误响应:com.amazonaws.services.s3.model.AmazonS3Exception:状态代码:403,AWS 服务:null,AWS 请求 ID:AD2F31F1133A650E,AWS 错误代码:AccessDenied。
我无法获取 s3object 本身。我将如何获得 S3 对象并重命名它的任何建议或想法。 感谢您的帮助。
【问题讨论】:
谢谢。我改为getS3Client().copyObject(getBucketName(), oldPhotoName, getBucketName(), "vignesh");
但现在我也遇到了同样的错误。 Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 874B8E8905B026CC, AWS Error Code: AccessDenied, AWS Error Message: Access Denied
你能帮忙吗。
这能回答你的问题吗? How to rename files and folder in Amazon S3?
【参考方案1】:
直接重命名S3对象is not possible
通过复制对象并删除原始对象来重命名对象
您可以使用例如复制和删除
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName,
keyName, bucketName, destinationKeyName);
s3client.copyObject(copyObjRequest);
s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
【讨论】:
以上代码只会复制文件,不会复制子文件夹。【参考方案2】:正如High6回答的那样,直接重命名s3是不可能的,但他的代码不会复制所有内容
我附上了一个代码sn-p,它将复制所有内容
代码正在运行,只需添加您的 aws 访问密钥和密钥
这是我在代码中所做的
-> 复制源文件夹内容(嵌套的子文件夹和文件夹)并粘贴到目标文件夹中
->复制完成后,删除源文件夹
package com.bighalf.doc.amazon;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectSummary;
public class Test
public static boolean renameAwsFolder(String bucketName,String keyName,String newName)
boolean result = false;
try
AmazonS3 s3client = getAmazonS3ClientObject();
List<S3ObjectSummary> fileList = s3client.listObjects(bucketName, keyName).getObjectSummaries();
//some meta data to create empty folders start
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
//some meta data to create empty folders end
//final location is the locaiton where the child folder contents of the existing folder should go
String finalLocation = keyName.substring(0,keyName.lastIndexOf('/')+1)+newName;
for (S3ObjectSummary file : fileList)
String key = file.getKey();
//updating child folder location with the newlocation
String destinationKeyName = key.replace(keyName,finalLocation);
if(key.charAt(key.length()-1)=='/')
//if name ends with suffix (/) means its a folders
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, destinationKeyName, emptyContent, metadata);
s3client.putObject(putObjectRequest);
else
//if name doesnot ends with suffix (/) means its a file
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName,
file.getKey(), bucketName, destinationKeyName);
s3client.copyObject(copyObjRequest);
boolean isFodlerDeleted = deleteFolderFromAws(bucketName, keyName);
return isFodlerDeleted;
catch (Exception e)
e.printStackTrace();
return result;
public static boolean deleteFolderFromAws(String bucketName, String keyName)
boolean result = false;
try
AmazonS3 s3client = getAmazonS3ClientObject();
//deleting folder children
List<S3ObjectSummary> fileList = s3client.listObjects(bucketName, keyName).getObjectSummaries();
for (S3ObjectSummary file : fileList)
s3client.deleteObject(bucketName, file.getKey());
//deleting actual passed folder
s3client.deleteObject(bucketName, keyName);
result = true;
catch (Exception e)
e.printStackTrace();
return result;
public static void main(String[] args)
intializeAmazonObjects();
boolean result = renameAwsFolder(bucketName, keyName, newName);
System.out.println(result);
private static AWSCredentials credentials = null;
private static AmazonS3 amazonS3Client = null;
private static final String ACCESS_KEY = "";
private static final String SECRET_ACCESS_KEY = "";
private static final String bucketName = "";
private static final String keyName = "";
//renaming folder c to x from key name
private static final String newName = "";
public static void intializeAmazonObjects()
credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY);
amazonS3Client = new AmazonS3Client(credentials);
public static AmazonS3 getAmazonS3ClientObject()
return amazonS3Client;
【讨论】:
以上是关于如何重命名 Amazon S3 存储桶中的文件? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何在不下载文件的情况下搜索amazon S3存储桶中的文件内容