javascript - 上传到亚马逊s3非常慢需要时间

标签 javascript python-2.7 amazon-s3 boto3

我正在使用带有 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 - 上传到亚马逊s3非常慢需要时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48439646/

相关文章:

javascript - Firefox 引导扩展,导入顶级时的竞争条件?

javascript - 应用程序关闭时监听 firebase 数据库更改

python - 在 re.search 中重用已编译的正则表达式

python - 如何将 "source"文件转换为 python 脚本

javascript - 创建 lambda 函数以生成 PDF 文件缩略图

javascript - 对象的数组属性

javascript - 如何使用 jquery ui 在表体 td 中进行可拖动和可排序

python - 我认为它应该包含每个 "append"的不同列表,但事实并非如此

amazon-s3 - 在 CloudFormation 或 serverless.yml 中提供 OriginAccessIdentity 引用

python - 如何判断 boto 使用的是 SSLv3 还是 TLS?