简体   繁体   中英

Rename key using boto3 within the same s3 bucket

I'm trying to re-key uploaded files so that they move from a 'temp-uploads' "folder" to a 'new-uploads' "folder.

This is my attempt thus far --- I get no error, but the files are still in 'temp-uploads'

  bucket = s3.Bucket(s3_bucket)
    temp_prefix = 'temp-uploads/' + packageInstance.prefix + '/'
    new_prefix = 'new-uploads/' + packageInstance.prefix + '/'
    for obj in bucket.objects.filter(Prefix=temp_prefix):
        old_source = {'Bucket': bucket,
                      'Key': obj.key}
        new_key = obj.key.replace(temp_prefix, new_prefix)
        new_obj = bucket.Object(new_key)
        new_obj.copy(old_source)

This seems to copy the actual file from temp-uploads/ to new-uploads/ , so the old file still exists in the old location.

Then, files uploaded by Django are stored as a reference in a FileField . That means Django generates an URL from a previously uploaded file. If the file is moved on the bucket outside of Django, it has no way to know it happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM