Google Cloud Storage - 如何从 Python 3 上传文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37003862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:40:18  来源:igfitidea点击:

Google Cloud Storage - How to upload a file from Python 3?

pythonpython-3.xgoogle-cloud-storagebotogoogle-cloud-platform

提问by aknuds1

How can I upload a file to Google Cloud Storagefrom Python 3? Eventually Python 2, if it's infeasible from Python 3.

如何将文件从 Python 3上传到Google Cloud Storage?最终是 Python 2,如果它在 Python 3 中不可行的话。

I've looked and looked, but haven't found a solution that actually works. I tried boto, but when I try to generate the necessary .boto file through gsutil config -e, it keeps saying that I need to configure authentication through gcloud auth login. However, I have done the latter a number of times, without it helping.

我看了又看,但还没有找到真正有效的解决方案。我尝试了boto,但是当我尝试通过 .boto 生成必要的 .boto 文件时gsutil config -e,它一直说我需要通过 .boto配置身份验证gcloud auth login。但是,我已经做了很多次后者,但没有帮助。

回答by aknuds1

Use the standard gcloudlibrary, which supports both Python 2 and Python 3.

使用支持 Python 2 和 Python 3的标准gcloud库。

Example of Uploading File to Cloud Storage

上传文件到云存储示例

from gcloud import storage
from oauth2client.service_account import ServiceAccountCredentials
import os


credentials_dict = {
    'type': 'service_account',
    'client_id': os.environ['BACKUP_CLIENT_ID'],
    'client_email': os.environ['BACKUP_CLIENT_EMAIL'],
    'private_key_id': os.environ['BACKUP_PRIVATE_KEY_ID'],
    'private_key': os.environ['BACKUP_PRIVATE_KEY'],
}
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
    credentials_dict
)
client = storage.Client(credentials=credentials, project='myproject')
bucket = client.get_bucket('mybucket')
blob = bucket.blob('myfile')
blob.upload_from_filename('myfile')

回答by adam shamsudeen

A simple function to upload files to a gcloud bucket.

将文件上传到 gcloud 存储桶的简单功能。

from google.cloud import storage

def upload_to_bucket(blob_name, path_to_file, bucket_name):
    """ Upload data to a bucket"""

    # Explicitly use service account credentials by specifying the private key
    # file.
    storage_client = storage.Client.from_service_account_json(
        'creds.json')

    #print(buckets = list(storage_client.list_buckets())

    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.upload_from_filename(path_to_file)

    #returns a public url
    return blob.public_url

You can generate a credential file using this link: https://cloud.google.com/storage/docs/reference/libraries?authuser=1#client-libraries-install-python

您可以使用此链接生成凭证文件:https: //cloud.google.com/storage/docs/reference/libraries?authuser=1#client-libraries-install-python

Asynchronous Example:

异步示例:

import asyncio
import aiohttp
# pip install aiofile
from aiofile import AIOFile
# pip install gcloud-aio-storage
from gcloud.aio.storage import Storage 

BUCKET_NAME = '<bucket_name>'
FILE_NAME  = 'requirements.txt'
async def async_upload_to_bucket(blob_name, file_obj, folder='uploads'):
    """ Upload csv files to bucket. """
    async with aiohttp.ClientSession() as session:
        storage = Storage(service_file='./creds.json', session=session) 
        status = await storage.upload(BUCKET_NAME, f'{folder}/{blob_name}', file_obj)
        #info of the uploaded file
        # print(status)
        return status['selfLink']


async def main():
    async with AIOFile(FILE_NAME, mode='r') as afp:
        f = await afp.read()
        url = await async_upload_to_bucket(FILE_NAME, f)
        print(url)


# Python 3.6
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

# Python 3.7+
# asyncio.run(main()) 

回答by James Siva

Imports the Google Cloud client library (need credentials)

导入 Google Cloud 客户端库(需要凭据

from google.cloud import storage
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:/Users/siva/Downloads/My First Project-e2d95d910f92.json" 

Instantiates a client

实例化一个客户端

storage_client = storage.Client()

buckets = list(storage_client.list_buckets())

bucket = storage_client.get_bucket("ad_documents")//your bucket name

blob = bucket.blob('/chosen-path-to-object/{name-of-object}')
blob.upload_from_filename('D:/Download/02-06-53.pdf')
print(buckets)

回答by Dragos Vasile

When installing Google Cloud Storage API:

安装 Google Cloud Storage API 时:

pip install google-cloud

pip install google-cloud

will throw a ModuleNotFoundError:

会抛出一个ModuleNotFoundError

    from google.cloud import storage
ModuleNotFoundError: No module named 'google'

Make sure you install as inCloud Storage Client Libraries Docs:

确保按照Cloud Storage Client Libraries Docs 中的方式安装

pip install --upgrade google-cloud-storage

pip install --upgrade google-cloud-storage