使用Python将多个Web文件上载到云存储
我正在尝试使用python将多个web文件上载到存储桶。我已经设置了一个服务帐户来启用凭据,因此它应该可以正常工作。但是,每次尝试运行此代码时,我都会收到以下错误: 禁止:403获取:xxx@yyy.zzz.com没有storage.Bucket.get访问voterfile oh的权限 我能在这个问题上得到帮助吗?我无法从中收集任何有用的信息,提问者也没有得到解决问题的答案使用Python将多个Web文件上载到云存储,python,google-cloud-platform,google-cloud-functions,google-cloud-storage,Python,Google Cloud Platform,Google Cloud Functions,Google Cloud Storage,我正在尝试使用python将多个web文件上载到存储桶。我已经设置了一个服务帐户来启用凭据,因此它应该可以正常工作。但是,每次尝试运行此代码时,我都会收到以下错误: 禁止:403获取:xxx@yyy.zzz.com没有storage.Bucket.get访问voterfile oh的权限 我能在这个问题上得到帮助吗?我无法从中收集任何有用的信息,提问者也没有得到解决问题的答案 def upload_voterfile(bucket_name): from google.cloud imp
def upload_voterfile(bucket_name):
from google.cloud import storage
from google.oauth2.service_account import Credentials
import os
import json
credentials_dict = {credentials}
with open('credentials.json', 'w') as json_file:
json.dump(credentials_dict, json_file)
credentials = Credentials.from_service_account_file('credentials.json')
client = storage.Client(credentials=credentials, project='oh-data-pipeline')
bucket = client.get_bucket(bucket_name)
county_list= ['Adams','Allen', 'Ashland', 'Ashtabula', 'Athens', 'Auglaize', 'Belmont', 'Brown', 'Butler', 'Carroll',
'Champaign', 'Clark', 'Clermont', 'Clinton', 'Columbiana', 'Coshocton', 'Crawford', 'Cuyahoga', 'Darke',
'Defiance', 'Delaware', 'Erie', 'Fairfield', 'Fayette', 'Franklin', 'Fulton', 'Gallia', 'Geauga', 'Greene',
'Guernsey', 'Hamilton', 'Hancock', 'Hardin', 'Harrison', 'Henry', 'Highland', 'Hocking', 'Holmes',
'Huron', 'Jackson', 'Jefferson', 'Knox', 'Lake', 'Lawrence', 'Licking', 'Logan', 'Lorain', 'Lucas', 'Madison',
'Mahoning', 'Marion', 'Medina', 'Meigs', 'Mercer', 'Miami', 'Monroe', 'Montgomery', 'Morgan', 'Morrow',
'Muskingum', 'Noble', 'Ottawa', 'Paulding', 'Perry', 'Pickaway', 'Pike', 'Portage', 'Preble', 'Putnam',
'Richland', 'Ross', 'Sandusky', 'Scioto', 'Seneca', 'Shelby', 'Stark', 'Summit', 'Trumbull', 'Tuscarawas',
'Union', 'Van Wert', 'Vinton', 'Warren', 'Washington', 'Wayne', 'Williams', 'Wood', 'Wyandot']
for f, g in zip(range(1, 89), county_list):
file = urllib.request.urlopen('https://www6.sos.state.oh.us/ords/f?p=VOTERFTP:DOWNLOAD::FILE:NO:2:P2_PRODUCT_NUMBER:{}'.format(f))
blob = bucket.blob('{}'.format(g))
blob.upload_from_string(data=file.read(), content_type="text/plain")
if __name__ == "__main__":
upload_voterfile(bucket_name='voterfile-oh')
我已经能够复制您的问题,以下是我遵循的步骤:
gcloud iam服务帐户密钥创建[FILE\u NAME].json--iam帐户[NAME]@[PROJECT\u ID].iam.gserviceaccount.com
到文件get\u bucket
方法获取bucket对象。这将查询云存储,需要对您的bucket获取权限,但存储对象创建者角色不包括获取权限
要解决您的问题,只需使用以下代码更改第14行:bucket=client.bucket(bucket\u name)
,它直接创建一个bucket对象,而不与云存储交互,因此不会引发权限错误,请参阅相应的
另一种解决方案可能是将服务帐户角色更改为存储对象管理员,因为它包括获取权限。您分配给了哪些角色
xxx@yyy.zzz.com
对于拥有bucket的项目?在IAM下的谷歌云控制台中查找此信息。只需在@JohnHanley的评论中添加一点。。。确保分配给该服务帐户的角色有权执行您需要的操作,例如storage.bucket.get。这非常有帮助。请注意,在GCS中,写入者角色(如objectCreator)不会继承读取访问权限,因此可以拥有可以写入但不能读取的svc帐户。您可能还需要分配另一个具有storage.bucket.get的角色。或者,您可能根本不需要调用client.get_bucket()。这将从API获取有关bucket的所有信息。如果这只是以后创建blob的一种方法,那么您应该能够使用它的blob()方法bucket=bucket(客户端,name=bucket\u name)
或类似内容。您似乎发布了敏感/私人信息。请重置密码和/或撤销API密钥和令牌。您还可以编辑出信息,并为版主标记您的帖子,以编辑编辑历史记录。