0

I have to upload images in s3 bucket in my django project. I'm using boto3 to the do the same as follows:

def handle_uploaded_file(file, filename):

    s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
    check = s3.Bucket(bucket).put_object(Key=filename, Body=file,ContentType='image/png',ACL='public-read')
    return check

I'm calling this function in my API like this:

        if request.FILES and request.FILES.get('tagimage', None) is not None:
            tagimage = request.FILES['tagimage']
            tagimage_name = tagimage.name
            number = number_genarator()
            tagimage_name = str(number) + tagimage_name
            tag_upload = handle_uploaded_file(tagimage,tagimage_name)
            res['tagimage']=tag_upload
            record.tagimage = tagimage_name

But this process is taking too long as per me. To upload one image it's taking me three seconds barring the time needed for the other part of my API. Can someone suggest a faster way to upload images?

Megha Sirisilla
  • 151
  • 2
  • 12
  • What is the size of your image ? – Taek Jan 15 '20 at 12:17
  • @Taek The images that I'm trying to upload are in kbs. Not even mbs! – Megha Sirisilla Jan 15 '20 at 12:23
  • Which part of the view takes most of the time ? is it `handle_uploaded_file` ? Can you check with something like https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python – Taek Jan 15 '20 at 12:31
  • @Taek I did what you said and the time difference to upload the image is 3.60 seconds. My API without image uploading takes 1 second. – Megha Sirisilla Jan 15 '20 at 12:36
  • Then it must be coming from your network, the exact same function takes around a second for me. You could also try upgrading your packages, it might help.. – Taek Jan 15 '20 at 12:43
  • @Taek ok. Thanks for replying. I'll check if there's an issue with the network. – Megha Sirisilla Jan 15 '20 at 12:45
  • If you test the upload by using the AWS CLI `aws s3 cp` command from the same computer, how long does the upload take? – John Rotenstein Jan 15 '20 at 20:57

1 Answers1

0

Turns out the issue was due to the region I selected for my bucket. Once I changed the bucket region nearer to my physical location, API response time gradually decreased to less than 1 second. It was my first time working with AWS services, guess with experiences like these I'll get to know em better!

Megha Sirisilla
  • 151
  • 2
  • 12