2017-04-11 91 views
2

我将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,但仍然存在错误。

我该如何解决这类错误?

谢谢您的高级。

+0

这听起来像你的对象是不能公开访问。您需要将其标记为公共可读,无论是在控制台中还是在上载时设置ACL。 –

回答

0

我解决了它。这就好像@Brandon Yarbrough先生说的那样,这个桶的对象不是公众可读的。

为了使斗市民,从这个环节采取 - >https://github.com/GoogleCloudPlatform/gsutil/issues/419

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

请在此处查看更新的答案:https://stackoverflow.com/a/20162788/948942 – Incinerator