如何从GCP中的python脚本将文件上载到GCS?

如何从GCP中的python脚本将文件上载到GCS?,python,google-cloud-platform,google-cloud-storage,Python,Google Cloud Platform,Google Cloud Storage,我正试图将一个文件上传到GCS,但我遇到了一个权限问题,我不知道如何解决。从GCS中的存储桶中读取文件似乎不是问题。但是,我在上传时遇到了一些问题 client = storage.Client() bucket = client.get_bucket('fda-drug-label-data') blob = bucket.get_blob(f'fda-label-doc-links.csv') bt = blob.download_as_string() s = str(bt, 'utf-8

我正试图将一个文件上传到GCS,但我遇到了一个权限问题,我不知道如何解决。从GCS中的存储桶中读取文件似乎不是问题。但是,我在上传时遇到了一些问题

client = storage.Client()
bucket = client.get_bucket('fda-drug-label-data')
blob = bucket.get_blob(f'fda-label-doc-links.csv')
bt = blob.download_as_string()
s = str(bt, 'utf-8')
s = StringIO(s)
df = pd.read_csv(s)

df_doc_links = list(df['Link'])

a = pd.DataFrame([len(df_doc_links)])
a.to_csv('test.csv', index=False)
client = storage.Client()
bucket = client.get_bucket('fda-drug-label-data')
blob = bucket.blob('test.csv')
blob.upload_from_filename('test.csv')
这是我得到的信息:

Traceback (most recent call last):   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1567, in upload_from_file
    if_metageneration_not_match,   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1420, in _do_upload
    if_metageneration_not_match,   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1098, in _do_multipart_upload
    response = upload.transmit(transport, data, object_metadata, content_type)   File "/home/.../.local/lib/python3.7/site-packages/google/resumable_media/requests/upload.py", line 108, in transmit
    self._process_response(response)   File "/home/.../.local/lib/python3.7/site-packages/google/resumable_media/_upload.py", line 109, in _process_response
    _helpers.require_status_code(response, (http_client.OK,), self._get_status_code)   File "/home/.../.local/lib/python3.7/site-packages/google/resumable_media/_helpers.py", line 96, in require_status_code
    *status_codes google.resumable_media.common.InvalidResponse: ('Request failed with status code', 403, 'Expected one of', <HTTPSta tus.OK: 200>) During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "scrape.py", line 134, in <module>
    blob.upload_from_filename('test.csv')   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1655, in upload_from_filename
    if_metageneration_not_match=if_metageneration_not_match,   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1571, in upload_from_file
    _raise_from_invalid_response(exc)   File "/home/.../.local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 2620, in _raise_from_invalid_response
    raise exceptions.from_http_status(response.status_code, message, response=response) google.api_core.exceptions.Forbidden: 403 POST https://storage.googleapis.com/upload/storage/v1/b/fda-drug-label-da ta/o?uploadType=multipart: ('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)

您没有权限上载到服务帐户中的数据。请转到“IAM和管理”部分,并在“服务帐户”下为您的帐户分配权限角色。然后再次生成密钥。

您无需重新下载密钥。您添加到服务帐户的任何权限都会被现有密钥自动获取。@Gabewiss这是否适用于我们通过json文件进行身份验证?正确。如果您在创建服务帐户时,点击Create key并下载json文件,那么json文件中的承载令牌只是指向服务帐户的一个键。因此,您添加到服务帐户中的任何角色/权限都会被json文件自动获取。这是一个非常糟糕的建议!服务帐户密钥文件是安全的噩梦。这是一个简单的文件,您可以复制、通过电子邮件发送,甚至提交到GIt或public中。此外,你必须安全地存储它这是你的身份验证秘密,谷歌建议至少每90天轮换一次这个文件!如果您在本地环境中,只需执行gcloud auth应用程序默认登录,以便在测试中使用您自己的凭据。如果是在GCP上,请告诉我有关哪种服务的更多帮助。如果在GCP之外,这是唯一需要密钥文件的情况!!!