作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有 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
考虑到您的互联网连接,我认为速度没有问题......实际上应该需要更多时间
关于javascript - 上传到 amazon s3 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48439646/
我是一名优秀的程序员,十分优秀!