在 s3 存储桶中上传图像需要很长时间



我必须在我的 django 项目中上传 s3 存储桶中的图像。我正在使用 boto3 执行以下操作:

def handle_uploaded_file(file, filename):
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
check = s3.Bucket(bucket).put_object(Key=filename, Body=file,ContentType='image/png',ACL='public-read')
return check

我在我的 API 中调用这个函数,如下所示:

if request.FILES and request.FILES.get('tagimage', None) is not None:
tagimage = request.FILES['tagimage']
tagimage_name = tagimage.name
number = number_genarator()
tagimage_name = str(number) + tagimage_name
tag_upload = handle_uploaded_file(tagimage,tagimage_name)
res['tagimage']=tag_upload
record.tagimage = tagimage_name

但在我看来,这个过程花费的时间太长了。要上传一张图像,我需要三秒钟,除非我的 API 的另一部分所需的时间。 有人可以建议一种更快的上传图像的方法吗?

事实证明,问题是由于我为存储桶选择的区域造成的。一旦我将存储桶区域更改为靠近我的物理位置,API 响应时间就会逐渐减少到不到 1 秒。这是我第一次使用 AWS 服务,猜猜有了这样的经验,我会更好地了解它们!

最新更新