我正在使用带有boto3库的python-flask应用程序在Amazon S3上上传图像,但是大小为1MB-3MB,需要16-18秒的时间上传。
我所缺少的,请提出建议。
function readFile(evt) {
var file = evt.target.files[0];
var reader = new FileReader();
output_format = "jpg";
reader.onload = function(event) {
var i = document.getElementById("source_image");
i.src = event.target.result;
i.onload = function(){
image_width=$(i).width(),
image_height=$(i).height();
if(image_width > image_height){
i.style.width="320px";
}else{
i.style.height="300px";
}
i.style.display = "block";
console.log("Image loaded");
}
};
console.log("Filename:" + file.name);
console.log("Filesize:" + (parseInt(file.size) / 1024) + " Kb");
console.log("Type:" + file.type);
reader.readAsDataURL(file);
return false;
}
document.getElementById('fileinput').addEventListener('change', readFile, false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="upload_form" action="/updateuser" method="POST" enctype="multipart/form-data">
<label for="file">Choose file</label>
<input type="file" id="fileinput" />
<img id="source_image">
<input type="button" id="upload" value="uploadimage">
</form>
以下是我的python代码
我从配置文件导入了S3_KEY,S3_SECRET,S3_BUCKET,创建了boto3客户端实例,并使用s3.upload_fileobj()将文件上传到Amazon S3。
import boto3
from config import S3_KEY, S3_SECRET, S3_BUCKET
# Ajax function to support upload image call from UI
@app.route('/user/uploadimage',methods=['GET','POST'])
def uploadimage():
print "In uploadimage()"
starttime = int(round(time.time() * 1000))
print "Start Monitoring uploadimage()",starttime
try:
s3 = boto3.client(
"s3",
aws_access_key_id=S3_KEY,
aws_secret_access_key=S3_SECRET
)
except Exception as e:
print str(e)
try:
file = request.files['file']
s3.upload_fileobj(
file,
S3_BUCKET,
file.filename,
ExtraArgs={
"ACL": "public-read",
"ContentType": file.content_type
}
)
print "File uploaded successfully"
print "Stop Monitoring uploadimage()",(int(round(time.time() * 1000))-starttime)
except Exception as e:
print("Error while Saving Image on Amazon S3 : ", e)
最佳答案
0.5Mbps / 8 = 0,0625 MBps
1MB / 0,0625 ~ 16 sec
考虑到您的互联网连接,我认为速度没有问题...实际上应该花费更多时间