我试图pre-signed URL用 7dsys 过期时间生成。(据说最长持续时间为 7 天,AWS S3 预签名 URL 没有到期日)
# It is called and retruned in AWS Lambda
boto3.client('s3').generate_presigned_url(
'get_object',
Params={'Bucket': bucket, 'Key': object_key},
ExpiresIn=(60*60*24*7) # 7days
)
Run Code Online (Sandbox Code Playgroud)
然而,似乎不是保留了pre-signed URL7 天,而是仅仅保留了几个小时。之后pre-signed URL只返回 XML 格式。
<Error>
<Code>ExpiredToken</Code>
<Message>The provided token has expired.</Message>
.
.
.
</Error>
Run Code Online (Sandbox Code Playgroud)
甚至每次尝试的过期时间似乎都不一样,有时是 5 小时,有时是 12 小时。
我不知道为什么。
我只是boto3 的新手,我不明白如何获取我刚刚上传到 s3 amazon 的文件的 URL 链接。
请说清楚。
谢谢
import boto3
s3 = boto3.resource('s3')
data = open ('file.xlsx', 'rb')
s3.Bucket ('dimxxx1').put_object (Key='file.xlsx', Body=data)
Run Code Online (Sandbox Code Playgroud) 我正在使用django-storage(内部使用 Boto3)上传图像。我能够成功地做到这一点,我得到的返回 URL 是这种格式:
https://.s3.amazonaws.com/foo.jpg?Signature=&AWSAccessKeyId=&Expires=1513089114
其中还填写了 Signature 和 AWSAccessKeyId。
现在,我需要将这个 URL 直接提供给移动开发人员,我不能这么晚设置超时。我需要它很多年或可能总是可以访问。这样做的好方法是什么?解决办法是什么
从 SDK 文档(链接:https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#_createPresignedRequest)$expires中,参数应表示URL 应该过期。
因此,如果我指定 2 分钟作为过期时间,则 2 分钟后该 URL 应该无效。我的代码看起来像这样
<?php
$s3 = $this->cloudProvider->getClient(); // S3 client
$cmd = $s3->getCommand(
'GetObject',
[
'Bucket' => $this->getSdkBucket(), // Bucket name
'Key' => "$s3Name",
]
);
$urlReq = $s3->createPresignedRequest($cmd, $expirationTime); // $expirationTime is a Unix timestamp
Run Code Online (Sandbox Code Playgroud)
我得到了具有正确到期时间的网址(在我的例子中,客户端希望它是会话到期时间,而会话时间是 4 小时)
X-Amz-Content-Sha256=UNSIGNED-PAYLOAD
&X-Amz-Security-Token=long_string_goes_here
&X-Amz-Algorithm=AWS4-HMAC-SHA256
&X-Amz-Credential=another_string_goes_here
&X-Amz-Date=20200907T110127Z
&X-Amz-SignedHeaders=host
&X-Amz-Expires=14400 // This decreases depending on how long the user is logged in - max 4hrs
&X-Amz-Signature=another_string_here
Run Code Online (Sandbox Code Playgroud)
问题是这个url在4小时后才有效。
根据我在这个答案中读到的有关过期时间的内容 ( /sf/answers/4045488961/),URL的有效期为 6 …