如何从谷歌云存储桶下载一个对象?

时间:2017-04-11 03:29:08

标签: python google-app-engine google-cloud-storage

我已将CSV文件test.csv上传到Google云端存储空间。生成的public_url就像这样

https://storage.googleapis.com/mybucket/test-2017-04-11-025727.csv

最初`test.csv'有一些行和列包含这样的数字

6,148,72,35,0,33.6,0.627,50,1 
8,183,64,0,0,23.3,0.672,32,1
...
...

我通过参考书架教程上传了该文件 - > https://github.com/GoogleCloudPlatform/getting-started-python没有6-pubsub。上传的文件将保存并添加时间戳。

现在,我想使用requests

下载我上传到存储桶的文件

以下是我一直致力于的工作。原始样本位于6-pubsub/bookshelf/crud.py。下面是我根据原始样本编辑过的脚本。

from machinelearning import get_model, oauth2, storage, tasks
from flask import Blueprint, current_app, redirect, render_template, request, session, url_for

import requests
import os

...
...

crud = Blueprint('crud', __name__)

save_folder = 'temp/'

def upload_csv_file(file):
    ...
    ...
    return public_url

...
...
@crud.route('/add', methods=['GET', 'POST']) 
def add():
    data = request.form.to_dict(flat=True)

    # This will upload the file that I pushed from local directory to GCS

    if request.method == 'POST':
        csv_url = upload_csv_file(request.files.get('file'))

        if csv_url:
            data['csvUrl'] = csv_url

    # I think this is not working. This should download back the file and save it to a temporary folder inside current working directory

    response = requests.get(public_url)
    if not os.path.exists(save_folder):
        os.makedirs(save_folder)

    with open(save_folder + 'testdata.csv', 'wb') as f:
        f.write(response.content)

    ...
    ...

我打开文件夹temp并检查testdata.csv。它在CSV文件中向我显示了这样的错误。

<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>Anonymous users does not have storage.objects.get access to object mybucket/test-2017-04-11-025727.csv.</Details></Error>

我希望testdata.csv拥有与test.csv相同的内容,但事实并非如此。

我已经在config.py上重新检查了我的OAuth客户端和密码桶ID,但错误仍然存​​在。

如何解决此类错误?

先谢谢你。

1 个答案:

答案 0 :(得分:0)

我解决了。就像@Brandon Yarbrough先生所说,桶的对象不是公开可读的。

要公开广告,请点击此链接 - &gt; https://github.com/GoogleCloudPlatform/gsutil/issues/419

gsutil defacl set public-read gs://<bucket_name>