Amazon S3中是否有重命名文件和文件夹的功能?欢迎提出相关建议。


当前回答

您可以使用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>

其他回答

S3中没有重命名文件的直接方法。你所要做的就是复制现有文件的新名称(只要设置目标键),然后删除旧的。

下面是重命名s3上文件的代码示例。由于spark o/p文件,我的文件是part-000*,然后我将其复制到相同位置的另一个文件名,并删除part-000*:

import boto3
client = boto3.client('s3')
response = client.list_objects(
Bucket='lsph',
MaxKeys=10,
Prefix='03curated/DIM_DEMOGRAPHIC/',
Delimiter='/'
)
name = response["Contents"][0]["Key"]
copy_source = {'Bucket': 'lsph', 'Key': name}
client.copy_object(Bucket='lsph', CopySource=copy_source, 
Key='03curated/DIM_DEMOGRAPHIC/'+'DIM_DEMOGRAPHIC.json')
client.delete_object(Bucket='lsph', Key=name)

如果您想重命名s3文件夹中的许多文件,可以运行以下脚本。

    FILES=$(aws s3api list-objects --bucket your_bucket --prefix 'your_path' --delimiter '/'  | jq -r '.Contents[] | select(.Size > 0) | .Key' | sed '<your_rename_here>')
     for i in $FILES
     do
      aws s3 mv s3://<your_bucket>/${i}.gz s3://<your_bucket>/${i}
     done   

正如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;
}

}

这是现在可能的文件,选择文件,然后选择操作>重命名在GUI。

要重命名文件夹,你必须创建一个新文件夹,并选择旧文件夹的内容并复制/粘贴它(再次在“操作”下)