如何在Amazon S3中重命名文件和文件夹?

是否有任何function在Amazon S3中重命名文件和文件夹? 任何相关的build议也欢迎。

没有直接的方法来重新命名s3中的文件。 你需要做的是复制现有文件的新名称(只需设置目标键),并删除旧的。 谢谢

我只是testing这个,它的工作原理:

 aws s3 --recursive mv s3://<bucketname>/<folder_name_from> s3://<bucket>/<folder_name_to> 

我刚刚这个工作。 您可以像这样使用适用于PHP的AWS开发工具包:

 use Aws\S3\S3Client; $sourceBucket = '*** Your Source Bucket Name ***'; $sourceKeyname = '*** Your Source Object Key ***'; $targetBucket = '*** Your Target Bucket Name ***'; $targetKeyname = '*** Your Target Key Name ***'; // Instantiate the client. $s3 = S3Client::factory(); // Copy an object. $s3->copyObject(array( 'Bucket' => $targetBucket, 'Key' => $targetKeyname, 'CopySource' => "{$sourceBucket}/{$sourceKeyname}", )); 

http://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingPHP.html

 aws s3 cp s3://source_folder/ s3://destination_folder/ --recursive aws s3 rm s3://source_folder --recursive 

您可以使用AWS CLI命令来制作文件

没有办法通过GUI重命名一个文件夹,最快的(也是最简单的,如果你喜欢GUI)的方式来实现这个目的就是执行一个普通的旧版本。 要做到这一点:使用GUI在S3上创build新的文件夹,到旧的文件夹,全选,标记“复制”,然后导航到新的文件夹,并select“粘贴”。 完成后,删除旧的文件夹。

这个简单的方法是非常快的,因为它是从S3复制到它自己的(不需要重新上传或类似的东西),它也保持复制对象的权限和元数据,就像你期望的那样。

文件和文件夹实际上是S3中的对象。 您应该使用PUT OBJECT COPY来重命名它们。 请参阅http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html

由Naaz直接重新命名s3是不可能的。

我附上了一个将复制所有内容的代码片段

代码正在工作,只需添加您的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; } 

}

您可以使用AWS CLI或s3cmd命令重命名AWS S3存储桶中的文件和文件夹。

使用S3cmd,使用以下语法来重命名文件夹,

 s3cmd --recursive mv s3://<s3_bucketname>/<old_foldername>/ s3://<s3_bucketname>/<new_folder_name> 

使用AWS CLI,使用以下语法重命名文件夹,

 aws s3 --recursive mv s3://<s3_bucketname>/<old_foldername>/ s3://<s3_bucketname>/<new_folder_name>