GCS Generate_Signed_Url在加载后过期 [英] GCS Generate_Signed_Url expires upon loading
问题描述
我正在实现一些代码来为json文件中指定的某些图像生成一个签名的url,这是用于生成它们的方法
I am implementing some code to generate a signed url for some images that are specified in a json file, this is the method used to generate them
def geturl(image_name):
storage_client = storage.Client()
bucket = 'Bucket Name'
source_bucket = storage_client.get_bucket(bucket)
blobs = source_bucket.list_blobs()
for blob in blobs:
if image_name == blob.name:
url_lifetime = 3600
serving_url = blob.generate_signed_url(url_lifetime)
return serving_url
return
此后,它们被用于img scr<>,但是在加载页面时,图像不加载,并且在url之后,我收到错误消息
after this they are used in an img scr <>, however when loading the page the images do not load and following the url i receive the error
提供的令牌已过期请求签名已过期:1970-01-01T10:00:00 + 00:00
The provided token has expired Request signature expired at: 1970-01-01T10:00:00+00:00
即使更改生命周期,错误消息仍然存在
even when changing the lifetime the error message persists
推荐答案
您的参数 url_lifetime
未正确初始化.正确的含义是到期日期
,它是从格林尼治标准时间1970年1月1日开始的秒数.您的有效期限是从1970年开始的一个小时.
Your parameter url_lifetime
is not initialized correctly. The correct meaning is expiration date
which is a value in seconds from January 1, 1970 GMT. Your expiration was one hour into 1970.
正确的方法是当前时间+ 3600
秒.
有许多获取当前时间的方法.示例: int(time.time())
返回以您的时区为单位的当前时间(以秒为单位).通常,您将需要将当前时间转换为格林尼治标准时间,然后获取秒数.
There are many ways to get the current time. Example: int(time.time())
which returns the current time in your timezone in seconds. Typically you will want to convert the current time to GMT and then grab the seconds.
注意:在这个答案中,我使用的格林尼治标准时间与UTC用法相同.
Note: In this answer, I am using GMT in the same usage as UTC.
from datetime import timezone, datetime
int(datetime.now(tz=timezone.utc).timestamp()
这篇关于GCS Generate_Signed_Url在加载后过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!