De Wiki inetshell
Saltar a: navegación, buscar

Obtener URL de un archivo

https://stackoverflow.com/questions/33809592/upload-to-amazon-s3-using-boto3-and-return-public-url

import boto3
from boto3.s3.transfer import S3Transfer

credentials = { 
    'aws_access_key_id': aws_access_key_id,
    'aws_secret_access_key': aws_secret_access_key
}

client = boto3.client('s3', 'us-west-2', **credentials)
transfer = S3Transfer(client)

transfer.upload_file('/tmp/myfile', bucket, key,
                     extra_args={'ACL': 'public-read'})

file_url = '%s/%s/%s' % (client.meta.endpoint_url, bucket, key)


Subir archivo

https://www.digitalocean.com/community/questions/how-to-upload-an-object-to-digital-ocean-spaces-using-python-boto3-library

import boto3

session = boto3.session.Session()
client = session.client('s3',
                        region_name='nyc3',
                        endpoint_url='https://nyc3.digitaloceanspaces.com',
                        aws_access_key_id='ACCESS_KEY',
                        aws_secret_access_key='SECRET_KEY')

client.upload_file('/path/to/file.ext',  # Path to local file
                   'my-space',  # Name of Space
                   'file.ext')  # Name for remote file


Como modificar ACL para hacer publico un archivo

https://stackoverflow.com/questions/47661177/using-boto3-how-to-put-a-publicly-readable-object-into-s3-or-digitalocean-spac

import boto3
boto_session = boto3.session.Session()
boto_client = boto_session.client('s3',
                    region_name='nyc3',
                    endpoint_url='https://nyc3.digitaloceanspaces.com',
                    aws_access_key_id='MY_SECRET_ACCESS_KEY_ID',
                    aws_secret_access_key='MY_SECRET_ACCESS_KEY')

boto_client.upload_file( FILE_PATHNAME, BUCKETNAME, OBJECT_KEYNAME )
boto_client.put_object_acl( ACL='public-read', Bucket=BUCKETNAME, Key=OBJECT_KEYNAME )