django boto3: NoCredentialsError — Unable to locate credentials

后端 未结 1 1015
耶瑟儿~
耶瑟儿~ 2021-01-18 22:02

I am trying to use boto3 in my django project to upload files to Amazon S3. Credentials are defined in settings.py:

AWS_ACCESS_KEY          


        
相关标签:
1条回答
  • 2021-01-18 22:36

    This is what I use for a direct upload, i hope it provides some assistance.

    import boto
    from boto.exception import S3CreateError
    from boto.s3.connection import S3Connection
    
    conn = S3Connection(settings.AWS_ACCESS_KEY,
                        settings.AWS_SECRET_KEY,
                        is_secure=True)
    try:
        bucket = conn.create_bucket(settings.S3_BUCKET)
    except S3CreateError as e:
        bucket = conn.get_bucket(settings.S3_BUCKET)
    
    k = boto.s3.key.Key(bucket)
    k.key = filename
    k.set_contents_from_filename(filepath)
    

    Not sure about (a) but django is very flexible with file management.

    Regarding (b) you can also sign the upload and do it directly from the client to reduce bandwidth usage, its quite sneaky and secure too. You need to use some JavaScript to manage the upload. If you want details I can include them here.

    0 讨论(0)
提交回复
热议问题