[Django]-Move file without opening in django-storages and S3

5👍

You can subclass the S3Boto3Storage class and add a method that copies files from a from_path to a to_path

from storages.backends.s3boto3 import S3Boto3Storage

class MyS3Storage(S3Boto3Storage):
    def copy(self, from_path, to_path):
        from_path = self._normalize_name(self._clean_name(from_path))
        to_path = self._normalize_name(self._clean_name(to_path))

        copy_result = self.connection.meta.client.copy_object(
            Bucket=self.bucket_name,
            CopySource=self.bucket_name + "/" + from_path,
            Key=to_path)

        if copy_result['ResponseMetadata']['HTTPStatusCode'] == 200:
            True
        else:
            False

After that you can create your MyS3Storage object and invoke the copy file using the from and to path.

👤fram

Leave a comment