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
に再チェックしましたが、まだエラーがあります。
どのようにこの種のエラーを解決できますか?
ありがとうございます。
あなたのオブジェクトは、公にアクセスできないように聞こえます。コンソールに表示するか、ACLをアップロードするときにACLを設定することで、公開されているとマークすることができます。 –