[Solved]-How to copy file from one path to other using django-storages and amazon S3?

11👍

this way you do not download content to save it

# Bucket: bucket name
# Key: file path
from storages.backends.s3boto3 import S3Boto3Storage
s3 = S3Boto3Storage()

def copy(Bucket_A, Bucket_B, Key_A, Key_B):
    copy_source = {'Bucket': Bucket_A, 'Key': Key_A}
    s3.bucket.meta.client.copy(copy_source, Bucket_B, Key_B)
    s3.delete(Key_A)
    return Key_B

def move(Bucket_A, Bucket_B, Key_A, Key_B):
    copy(Bucket_A, Bucket_B, Key_A, Key_B)
    s3.delete(Key_A)
    return Key_B
👤Sorker

9👍

You can use Django’s ContentFile to do this

from django.core.files.base import ContentFile

new_file = ContentFile(original_file.read())
new_file.name = original_file.name

example = Example()
example.file = new_file
example.save()

Leave a comment