简体   繁体   中英

AWS S3 Download and Upload using TemporaryFile

I need to download all content (including versions) of an Amazon S3 Bucket and upload in other Amazon S3 Bucket. Don't tell me to use aws, I just can't use.

I use tempfile.TemporaryFile for this, it apparently works, the print show that the file object has the right content inside, but the uploaded files are empty (zero bytes).

with tempfile.TemporaryFile() as data:
    sourceUser.download_fileobj('source-bucket',key,data)
    # next 2 lines was just to check the content of the file
    data.seek(0)
    print (data.read())
    destinationUser.upload_fileobj(data,'destination-bucket',key)

I have the same requirement, How do I pass the NamedTemporaryFile to the upload s3

Not sure How to pass the NamedTemporaryFileName to output=f'{file_name}.gpg' and to the load_file function --> filename=f_source.name

with tempFile.NamedTemporaryFile("wb") as f_source:
                s3_client.download_fileobj(s3_bucket, s3_key, f_source)
                logger.info(f'{s3_key} file downloaded successfully to local {f_source}')
                f_source.flush()
                file_name = self.s3_key.split('/')[-1]
                gpg = gnupg.GPG()
                key_data = open(key_path).read()
                import_result = gpg.import_keys(key_data)
                f_source.seek(0)
                with open(f_source.name, 'r+b') as f:
                    status = gpg.encrypt_file(
                        file=f,
                        recipients=[recipient],
                        output=f'{file_name}.gpg',
                    )

                    s3_hook.load_file(
                        filename=f_source.name,
                        key=s3_key,
                        bucket_name=s3_bucket,
                        replace=True
                    )

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