Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python为云存储中的对象生成签名URL_Python_Google Cloud Storage - Fatal编程技术网

使用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 =

我正在尝试使用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 = 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)

希望这能帮助别人

  • 浏览到API和服务的->凭据->创建凭据->服务帐户->提供适当的服务帐户名称并指定云存储管理员作为角色。下载密钥(.json文件)

  • 浏览到云存储Bucket,确保在该Bucket的permissions选项卡中存在上面创建的服务帐户。如果没有,请单击添加成员,添加上面创建的服务帐户并分配云存储管理员角色

  • 使用上面@fervelet给出的代码并获取签名的URL


  • 希望这能帮助别人

  • 浏览到API和服务的->凭据->创建凭据->服务帐户->提供适当的服务帐户名称并指定云存储管理员作为角色。下载密钥(.json文件)

  • 浏览到云存储Bucket,确保在该Bucket的permissions选项卡中存在上面创建的服务帐户。如果没有,请单击添加成员,添加上面创建的服务帐户并分配云存储管理员角色

  • 使用上面@fervelet给出的代码并获取签名的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是否足够?我看了一些教程,它说存储管理员是应该被赋予的角色,因为它可以完全访问地面军事系统。