2017-06-22 136 views
0

我試圖將圖像上傳到AWS,作爲base64字符串或圖像,但上傳後圖像損壞/變黑。Python將圖像上傳到AWS - 圖像變黑/損壞

for item in os.listdir(os.path.join(app.config['UPLOAD_FOLDER'])): 
    if item.startswith(str(current_user.id)): 
     with open(os.path.join(app.config['UPLOAD_FOLDER'], item)) as thefile: 
      data = base64.b64encode(thefile.read()) 
      upload_image_to_aws_from_image_v3('MYBUCKET', "userimg/", data, new_zimmer, "hauptbild", new_zimmer.stadt, new_zimmer.id) 

對AWS的方法:

def upload_image_to_aws_from_image_v3(bucketname, bucketpath, image_from_form, model_to_change, model_column, model_addon_for_name, model_addon_for_name_id): 

    s3 = boto.connect_s3(app.config['MY_AWS_ID'], app.config['MY_AWS_SECRET'], host='s3.eu-central-1.amazonaws.com') 

    bucket_name = bucketname 
    bucket = s3.get_bucket(bucket_name) 
    k = Key(bucket) 

    #file_contents = image_from_form.read() 

    the_filename = "zimmer-" + model_addon_for_name.lower().strip() + str(model_addon_for_name_id) + ".jpg" 

    filename_hauptbild = secure_filename(the_filename)  

    k.key = bucketpath + filename_hauptbild 

    k.set_contents_from_string(image_from_form) 

    setattr(model_to_change, model_column, filename_hauptbild) 

回答

0

請嘗試以下,看看你得到更好的結果:

import boto 

def upload_image_to_aws_from_image_v3(bucketname, bucketpath, image_from_form, model_to_change, model_column, model_addon_for_name, model_addon_for_name_id): 

    s3 = boto.connect_s3(app.config['MY_AWS_ID'], app.config['MY_AWS_SECRET'], host='s3.eu-central-1.amazonaws.com') 
    bucket = s3.get_bucket(bucket_name) 

    the_filename = "zimmer-" + model_addon_for_name.lower().strip() + str(model_addon_for_name_id) + ".jpg" 

    filename_hauptbild = secure_filename(the_filename) 

    k = bucket.new_key(key_name) 
    k.set_contents_from_string(image_from_form) 
    k.set_contents_from_filename(path_to_file) 
    return k