1

Hello Awesome People!

I will try to be clear with my question.

All of my media files are uploaded to AWS, I created a view that allows each user to download images. Before, I did it without the boto that summed up

"this back-end doesn't support absolute path."

And now, after some research, I'm using the s3 boto3 connection.

Model

class Photo(models.Model):
     file = models.ImageField(upload_to="uploaded_documents/")
     total_download = models.PositiveIntegerField()

View

def download_file(request,id):  
     photo = get_object_or_404(Photo,id=id)
     photo.total_download += 1
     photo.save()

     path = os.path.basename(photo.file.name)
     # path = '/media/public/uploaded_documents/museum.jpg'

     client = boto3.client('s3',aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                 aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)


     resource = boto3.resource('s3')
     bucket = resource.Bucket(settings.AWS_STORAGE_BUCKET_NAME)

     bucket.download_file(path, 'my_local_image.jpg')

Here I don't know what to do to trigger it. when I run it, I get the following error:

NoCredentialsError at /api/download-file/75
Exception Type: NoCredentialsError
Exception Value: Unable to locate credentials

UPDATE

I use the credentials in resources instead of client

client = boto3.client('s3')
resource = boto3.resource('s3',aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                 aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)

and it seems to be authenticated. But now I got the error:

Exception Type: ClientError
Exception Value: An error occurred (404) when calling the HeadObject operation: Not Found

Eu Chi
  • 533
  • 1
  • 6
  • 22
  • do you have credentials in settings? are they imported? – AlexW Aug 07 '18 at 15:48
  • Yes, I have them. Do i have to do it in `boto3.client()` or `boto3.resource()`? – Eu Chi Aug 07 '18 at 15:53
  • try suggestions in this thread https://stackoverflow.com/questions/33297172/boto3-error-botocore-exceptions-nocredentialserror-unable-to-locate-credential – AlexW Aug 07 '18 at 15:55
  • see my updates in the question, please! Thank you for your suggestion – Eu Chi Aug 07 '18 at 15:57
  • it seems like the file isnt at the path you expect it to be, can you check the path in s3? – AlexW Aug 07 '18 at 16:01
  • it works @AlexW, the path was incorrect, you point me in the right direction. can you elaborate all your comments in one answer so I can mark it as accepted. – Eu Chi Aug 07 '18 at 16:47
  • youre welcome, glad you got it working – AlexW Aug 07 '18 at 16:50

1 Answers1

1

please try looking at the answers in:

Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

also check that the file path is correct in S3

AlexW
  • 2,843
  • 12
  • 74
  • 156