使用python为云存储中的对象生成签名URL
我正在尝试使用python为云存储中的对象生成签名URL使用python为云存储中的对象生成签名URL,python,google-cloud-storage,Python,Google Cloud Storage,我正在尝试使用python为云存储中的对象生成签名URL import datetime as dt ini_time_for_now = dt.datetime.now() expiration_time = ini_time_for_now + dt.timedelta(minutes = 15) print(expiration_time) client = storage.Client() bucket = client.get_bucket(bucketname) blob =
import datetime as dt
ini_time_for_now = dt.datetime.now()
expiration_time = ini_time_for_now + dt.timedelta(minutes = 15)
print(expiration_time)
client = storage.Client()
bucket = client.get_bucket(bucketname)
blob = bucket.blob(pdffolder)
blob.upload_from_filename(pdffilename)
url = blob.generate_signed_url('cred.json', bucketname,pdffolder,expiration=expiration_time)
我得到了这个错误
Traceback (most recent call last):
File "entryscript.py", line 18, in <module>
main()
File "entryscript.py", line 13, in main
testpdf(sys.argv[0], sys.argv[1])
File "/home/xxxx/GitHub/patch_python/local_test_scripts/patchQuick/Instant_analysis/test.py", line 504, in testpdf
url = blob.generate_signed_url('cred.json', bucketname,
pdffolder,expiration=expiration_time)
TypeError: generate_signed_url() got multiple values for argument 'expiration'`
回溯(最近一次呼叫最后一次):
文件“entryscript.py”,第18行,在
main()
文件“entryscript.py”,第13行,在main中
testpdf(sys.argv[0],sys.argv[1])
testpdf中的文件“/home/xxxx/GitHub/patch\u python/local\u test\u scripts/patchQuick/Instant\u analysis/test.py”,第504行
url=blob.generate\u signed\u url('cred.json',bucketname,
PDF文件夹,过期=过期时间)
TypeError:generate\u signed\u url()为参数“expiration”获取了多个值`
有人能告诉我我做错了什么吗。事实上,您的代码无法正常工作,因为您没有根据其属性正确使用
generate\u signed\u url()
方法。此外,我认为您将blob
对象的方法与所示的示例方法混淆了:
<> P>另一件事你应该考虑的是到期日应该在<强> UTC/<强>
以下代码从已创建的对象创建一个签名URL
,但您可以修改它以满足您的要求:
from google.oauth2 import service_account
from google.cloud import storage
from datetime import datetime, timezone, timedelta
#Define the service account key and project id
KEY='path/to/key.json'
PROJECT='PROJECT_ID'
#create a credential to initialize the Storage client
credentials = service_account.Credentials.from_service_account_file(KEY)
client = storage.Client(PROJECT,credentials)
#Define your Storage bucket and blob
bucketname = "BUCKET_NAME"
file = "BLOB_NAME"
#Get the time in UTC
ini_time_for_now = datetime.now(timezone.utc)
#Set the expiration time
expiration_time = ini_time_for_now + timedelta(minutes = 1)
#Initialize the bucket and blob
bucket = client.get_bucket(bucketname)
blob = bucket.get_blob(file)
#Get the signed URL
url = blob.generate_signed_url(expiration=expiration_time)
#Print the URL
print (url)
实际上,您的代码将无法工作,因为您没有根据其属性正确使用
generate\u signed\u url()
方法。此外,我认为您将blob
对象的方法与所示的示例方法混淆了:
<> P>另一件事你应该考虑的是到期日应该在<强> UTC/<强>
以下代码从已创建的对象创建一个签名URL
,但您可以修改它以满足您的要求:
from google.oauth2 import service_account
from google.cloud import storage
from datetime import datetime, timezone, timedelta
#Define the service account key and project id
KEY='path/to/key.json'
PROJECT='PROJECT_ID'
#create a credential to initialize the Storage client
credentials = service_account.Credentials.from_service_account_file(KEY)
client = storage.Client(PROJECT,credentials)
#Define your Storage bucket and blob
bucketname = "BUCKET_NAME"
file = "BLOB_NAME"
#Get the time in UTC
ini_time_for_now = datetime.now(timezone.utc)
#Set the expiration time
expiration_time = ini_time_for_now + timedelta(minutes = 1)
#Initialize the bucket and blob
bucket = client.get_bucket(bucketname)
blob = bucket.get_blob(file)
#Get the signed URL
url = blob.generate_signed_url(expiration=expiration_time)
#Print the URL
print (url)
希望这能帮助别人
希望这能帮助别人
我遇到了完全相同的错误-出于某种原因,我返回测试的URL将
&
HTML编码为&导致此错误的代码>
Traceback (most recent call last):
File "entryscript.py", line 18, in <module>
main()
File "entryscript.py", line 13, in main
testpdf(sys.argv[0], sys.argv[1])
File "/home/xxxx/GitHub/patch_python/local_test_scripts/patchQuick/Instant_analysis/test.py", line 504, in testpdf
url = blob.generate_signed_url('cred.json', bucketname,
pdffolder,expiration=expiration_time)
TypeError: generate_signed_url() got multiple values for argument 'expiration'`
对您来说可能不是同一个问题,只是以防万一。我遇到了完全相同的错误-出于某种原因,我返回测试的URL将&
HTML编码为&导致此错误的代码>
Traceback (most recent call last):
File "entryscript.py", line 18, in <module>
main()
File "entryscript.py", line 13, in main
testpdf(sys.argv[0], sys.argv[1])
File "/home/xxxx/GitHub/patch_python/local_test_scripts/patchQuick/Instant_analysis/test.py", line 504, in testpdf
url = blob.generate_signed_url('cred.json', bucketname,
pdffolder,expiration=expiration_time)
TypeError: generate_signed_url() got multiple values for argument 'expiration'`
对您来说可能不是同一个问题,只是以防万一。尝试将过期时间
强制转换为str
对象。我尝试过,但同样的错误也存在。我认为您还需要正确的权限,您可以找到云存储的权限列表。尝试将过期时间
强制转换为str
对象。我尝试过,但同样的错误也存在。我认为您还需要正确的权限,您可以找到云存储的权限列表。您的权利,我实际上与其他文档混淆了。我创建了一个服务帐户凭据,并将其分配为存储管理员角色,但我得到一个错误,即它确实具有存储,获取权限。您在执行代码时是否收到错误?获取bucket或文件时是否显示?我获取错误禁止:403获取https://storage.googleapis.com/storage/v1/b/bucketname?projection=noAcl: 服务帐户-name@projectid.iam.gserviceaccount.com没有storage.bucket。获取Google云存储bucket的访问权限。
获取bucket时。是,这似乎是一个许可问题。我建议您检查服务帐户是否具有正确的权限,以及您是否在代码中使用该SA。最后,请确保在代码中设置了正确的项目id,并且bucket与SA位于同一个项目中。将角色设置为Storage Admin是否足够?我看了一些教程,上面说Storage admin是要授予的角色,因为它提供了对GCS的完全访问权限。你说的对,我实际上对其他文档感到困惑。我创建了一个服务帐户凭据,并将其分配为存储管理员角色,但我得到一个错误,即它确实具有存储,获取权限。您在执行代码时是否收到错误?获取bucket或文件时是否显示?我获取错误禁止:403获取https://storage.googleapis.com/storage/v1/b/bucketname?projection=noAcl: 服务帐户-name@projectid.iam.gserviceaccount.com没有storage.bucket。获取Google云存储bucket的访问权限。
获取bucket时。是,这似乎是一个许可问题。我建议您检查服务帐户是否具有正确的权限,以及您是否在代码中使用该SA。最后,请确保在代码中设置了正确的项目id,并且bucket与SA位于同一个项目中。将角色设置为Storage Admin是否足够?我看了一些教程,它说存储管理员是应该被赋予的角色,因为它可以完全访问地面军事系统。