我在一个电子商务网站上使用BackBlaze,我在这个网站上允许用户为给定的产品上传一组照片,我将图片连同缩略图上传到Backblaze,并将URL存储在Firestore中。
我遇到的问题是上传速度非常慢。当用户上传一张< 1MB的照片时,需要23-27秒。奇怪的是,一张7-10 17的照片大约需要15-17秒。这两种方式看起来都非常慢,最重要的是,下载速度有时也会非常慢。我把Cloudflare放在Backblaze桶的前面,试图加快加载时间,但是每几个页面加载一到两个缩略图(小于.5MB),加载5+秒。
这是我的路线:
@router.post('/productImage', response_model=UploadImageResponse, status_code=201)
def upload_product_image_route(file: UploadFile, Authorize: AuthJWT = Depends()):
Authorize.jwt_required()
image_content = file.file.read()
img = PIL_Image.open(io.BytesIO(image_content))
try:
name: str = str(uuid.uuid4())
thumbnail_url = create_thumbnail_for_image(img, name)
image_url = upload_image_to_cloud_storage(img, name)
return UploadImageResponse(thumbnail=thumbnail_url, image=image_url)
except FileUploadError as e:
raise HTTPException(status_code=500, detail='Failed to process image.')
finally:
img.close()
file.file.close()我添加了一些指标来查看哪个任务花费的时间最长,而create_thumbnail_for_image通常花费的时间少于5秒,但根据照片的不同,我的upload_image_to_cloud_storage需要15-30秒。
def upload_image_to_cloud_storage(image: PIL_Image, image_name: str) -> str:
s3 = _get_storage_resource()
buffer = io.BytesIO()
image.save(buffer, format=image.format)
buffer.seek(0)
try:
s3.Bucket(BUCKET_NAME).upload_fileobj(buffer, image_name)
return f'{BASE_HOST_URL}/{image_name}'
except Exception as e:
raise FileUploadError(e)
def create_thumbnail_for_image(image: PIL_Image, image_name: str, size: [int, int] = None) -> str:
if size is None:
size = [350, 350]
image_name = f'{size[0]}-{size[1]}-{image_name}'
f = image.format
img: PIL_Image = image.copy()
img.thumbnail((size[0], size[1]))
img.format = image.format
r = upload_image_to_cloud_storage(img, image_name)
img.close()
return r知道为什么这些上传速度会花这么长时间吗?
发布于 2022-09-09 01:31:06
我试图复制您的结果,因此我将您的代码简化为:
def upload_image_to_cloud_storage(image_name: str, size: int):
s3 = _get_storage_resource()
# Generate random data so results aren't skewed by compression
buffer = io.BytesIO(bytearray(map(random.getrandbits, (8,) * size)))
try:
s3.Bucket(BUCKET_NAME).upload_fileobj(buffer, image_name)
except Exception as e:
raise FileUploadError(e)
SIZES = [
500 * 1024, # 500 kB
10 * 1024 * 1024 # 10 MB
]
TRIALS = 10
times = [[], []]
for i in range(len(SIZES)):
for j in range(TRIALS):
name: str = str(uuid.uuid4())
start = time.perf_counter_ns()
print('.', end='', flush=True)
upload_image_to_cloud_storage(name, SIZES[i])
times[i].append(time.perf_counter_ns() - start)
print('', flush=True)
for i in range(len(SIZES)):
mean = sum(times[i]) / len(times[i])
print(f'Size: {SIZES[i]}, time: {mean // 1000000} ms')以下是研究结果:
..........
..........
Size: 512000, time: 1489.0 ms
Size: 10485760, time: 15186.0 ms顺便说一句,如果你想知道我是否有一个特别快速的网络连接,我的宽带被切断了,我的笔记本电脑被拴在我的手机上,所以这是通过5G。
当您尝试上述代码时会发生什么?
此外,您所在的位置,以及您的背燃帐户在哪个地区?
https://stackoverflow.com/questions/73601106
复制相似问题