زوہیب خان
زوہیب خان

Reputation: 452

Uploading image on AWS S3 using boto is having the size: 0 byte, Python, Flask

I am trying to upload the image to my bucket on AWS S3. Earlier it was working fine. But now the uploaded image is having the size 0 byte. I have tried rolling back to previous versions of my project on GitHub. But nothing seems to work now. I am stuck on this issue for 2 days now.

def upload_to_aws(local_file, bucket_name, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=BaseConfig.AWS_ACCESS_KEY_ID,
                      aws_secret_access_key=BaseConfig.AWS_SECRET_ACCESS_KEY)
    s3.upload_fileobj(local_file, bucket_name, s3_file)
    file_url = '%s/%s/%s' % (s3.meta.endpoint_url, bucket_name, s3_file)
    return file_url
from werkzeug.datastructures import FileStorage

parser = reqparse.RequestParser()
parser.add_argument('image', 
  type=FileStorage,
  required=True, 
  help='image is required',
  location='files'
)

class Classifier(Resource):
  def post(self):
    data = Classifier.parser.parse_args()
    image = data["image"]
    key_name = "some-key-name"
    upload_to_aws(image, BaseConfig.BUCKET_NAME, key_name)
    return {message: "uploaded successfully"}, 200

Upvotes: 2

Views: 1800

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269101

The upload_fileobj() function will upload a file-like object to S3. You would pass it a file object returned from an open() command.

If the image variable contains a filename, you should be using upload_file() instead.

Upvotes: 1

Related Questions