How to rename files and folder in Amazon S3?

后端 未结 19 2193
暖寄归人
暖寄归人 2020-11-28 03:01

Is there any function to rename files and folders in Amazon S3? Any related suggestions are also welcome.

相关标签:
19条回答
  • 2020-11-28 03:28

    You can use the AWS CLI commands to mv the files

    0 讨论(0)
  • 2020-11-28 03:29

    Here's how you do it in .NET, using S3 .NET SDK:

    var client = new Amazon.S3.AmazonS3Client(_credentials, _config);
    client.CopyObject(oldBucketName, oldfilepath, newBucketName, newFilePath);
    client.DeleteObject(oldBucketName, oldfilepath);
    

    P.S. try to use use "Async" versions of the client methods where possible, even though I haven't done so for readability

    0 讨论(0)
  • 2020-11-28 03:30

    There is no direct method to rename a file in S3. What you have to do is copy the existing file with a new name (just set the target key) and delete the old one.

    0 讨论(0)
  • 2020-11-28 03:31

    S3DirectoryInfo has a MoveTo method that will move one directory into another directory, such that the moved directory will become a subdirectory of the other directory with the same name as it originally had.

    The extension method below will move one directory to another directory, i.e. the moved directory will become the other directory. What it actually does is create the new directory, move all the contents of the old directory into it, and then delete the old one.

    public static class S3DirectoryInfoExtensions
    {
        public static S3DirectoryInfo Move(this S3DirectoryInfo fromDir, S3DirectoryInfo toDir)
        {
            if (toDir.Exists)
                throw new ArgumentException("Destination for Rename operation already exists", "toDir");
            toDir.Create();
            foreach (var d in fromDir.EnumerateDirectories())
                d.MoveTo(toDir);
            foreach (var f in fromDir.EnumerateFiles())
                f.MoveTo(toDir);
            fromDir.Delete();
            return toDir;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:32

    I've just got this working. You can use the AWS SDK for PHP like this:

    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

    0 讨论(0)
  • 2020-11-28 03:33

    There is no way to rename a folder through the GUI, the fastest (and easiest if you like GUI) way to achieve this is to perform an plain old copy. To achieve this: create the new folder on S3 using the GUI, get to your old folder, select all, mark "copy" and then navigate to the new folder and choose "paste". When done, remove the old folder.

    This simple method is very fast because it is copies from S3 to itself (no need to re-upload or anything like that) and it also maintains the permissions and metadata of the copied objects like you would expect.

    0 讨论(0)
提交回复
热议问题