2017-04-11 13 views
2

Google Cloud StorageのバケットにCSVファイルtest.csvをアップロードしました。その結果public_urlこのGoogle Cloud Storageバケットからオブジェクトをダウンロードするにはどうすればよいですか?

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> 

私はtest.csvのように同じ内容を持つことになりますtestdata.csvを期待していたが、それはしませんでした。

私はすでにOAuthクライアントと秘密のバケットIDをconfig.pyに再チェックしましたが、まだエラーがあります。

どのようにこの種のエラーを解決できますか?

ありがとうございます。

+0

あなたのオブジェクトは、公にアクセスできないように聞こえます。コンソールに表示するか、ACLをアップロードするときに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

関連する問題