AWS签名URL下的大型pdf文件无法在浏览器中打开。
创始人
2024-11-19 11:30:10
0

当使用AWS签名URL下载大型pdf文件时,可能会遇到浏览器无法打开的问题。这通常是由于超时或文件大小限制引起的。以下是解决此问题的步骤:

  1. 通过AWS S3控制台或AWS CLI设置响应头部。

在AWS控制台或AWS CLI中,设置Content-Disposition响应头内容为“attachment”即可。

aws s3 cp s3://myBucket/myLargeFile.pdf - • --response-content-disposition="attachment"

这将在下载时自动触发下载对话框。

  1. 使用分片下载。

将文件分片下载可避免下载的文件过大,从而提高下载速度。

以下是示例代码:

import boto3 import requests

s3 = boto3.resource('s3')

Get a client to interact with S3

client = boto3.client('s3')

Set the bucket and object key

bucket_name = 'myBucket' object_key = 'myLargeFile.pdf'

Generate a pre-signed URL for the S3 object

url = client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': object_key})

Open a new request to the pre-signed URL

req = requests.get(url, stream=True)

Get the total size of the file

total_size = int(req.headers.get('content-length', 0))

Define the chunk size you want to use

chunk_size = 1024 * 1024 # 1 MB

Define the number of chunks needed

num_chunks = total_size // chunk_size + (total_size % chunk_size > 0)

Define a function to read the response content in chunks

def read_chunk(): while True: data = req.raw.read(chunk_size, decode_content=True) if not data: break yield data

Stream the file in chunks to the client

response = app.response_class(read_chunk(), mimetype='application/pdf', headers={'Content-Disposition': 'inline'})

return response

通过这些步骤,您可以成功地在

相关内容

热门资讯

6分钟辅助!hhpoker是真... 6分钟辅助!hhpoker是真的假的,hhpoker真的有透视吗,演示教程(真是有挂)1、每一步都需...
第七分钟辅助!we poker... 第七分钟辅助!we poker插件,we poker免费辅助器,手筋教程(有挂方式)暗藏猫腻,小编详...
七分钟辅助!aa poker辅... 七分钟辅助!aa poker辅助包,pokemmo脚本辅助器下载,讲义教程(有挂秘籍)1、pokem...
第四分钟辅助!wepoker辅... 第四分钟辅助!wepoker辅助器安装包定制,aapoker透视脚本,大纲教程(有挂总结)所有人都在...
第四分钟辅助!wpk透视辅助靠... 第四分钟辅助!wpk透视辅助靠谱吗,wepoker透视苹果系统,诀窍教程(有挂猫腻);运wepoke...
七分钟辅助!aapoker透视... 七分钟辅助!aapoker透视脚本入口,wejoker开挂,学习教程(有挂教程)1、打开软件启动之后...
第四分钟辅助!淘宝买wepok... 第四分钟辅助!淘宝买wepoker透视有用吗,wpk俱乐部怎么作弊,积累教程(有挂规律)1、下载好淘...
6分钟辅助!we-poker软... 6分钟辅助!we-poker软件,购买的wpk辅助在哪里下载,积累教程(有挂技巧)购买的wpk辅助在...
第2分钟辅助!如何下载wepo... 第2分钟辅助!如何下载wepoker安装包,newpoker怎么安装脚本,教程书教程(今日头条)1、...
4分钟辅助!wepoker钻石... 4分钟辅助!wepoker钻石怎么看底牌,wepoker辅助器有哪些功能,法门教程(有挂详细)该软件...