我想把檔案從Google App Engine上傳到Google Cloud Storage。 我正在使用Pyhton 3.8和Flask。
app.yaml:
runtime: python38
requirements.txt
Flask==2.0.0;
google-cloud
google-cloud-storage
我試著用Flask(https://www.tutorialspoint.com/flask/flask_file_uploading.htm)將檔案上傳到/tmp(在App Engine臨時存盤(https://cloud.google.com/appengine/docs/standard/python3/using-temp-files))
然后我嘗試使用此代碼樣本從臨時存盤上傳到谷歌云存盤。(https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample)
但這給了我這樣的錯誤:
OSError: [Errno 30] 只讀的檔案系統。'file.xlsx'] 只讀檔案系統。
我也從這個樣本中嘗試了一下:(https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard/storage/api-client/main.py) 并給我同樣的錯誤。
然后我用 gsutil (https://cloud.google.com/storage/docs/uploading-objects#gsutil) 使用 subprocess.check_output (Return value of x = os.system(..)) 嘗試,仍然給我同樣的錯誤。
有人能幫助我嗎?我應該怎么做?
#################################################
已解決
#################################################
謝謝你Edo Akse,我的錯誤是使用"/tmp "而不是"/tmp/",非常感謝你的幫助。
下面是完整的代碼,它已經作業了。
from Flask import Flask, request
from werkzeug.utils import secure_filename
from google.cloud import storage
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "/tmp/"@app.route('/'))
def index()。
page = ''
<form action="/upload" method="post" enctype="multipart/form-data" >
<輸入名稱="file" class="custom-file-input" type="file">
<按鈕 type="提交" class="btn btn-success">提交</按鈕>。
</form>
''
return page
@app.route('/upload',methods=["POST"])
def upload()。
try:
if request.method == 'POST'/span>:
f = request.files['file']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
#Start Do Main Process Here[/span]。
TMP_FILE_NAME = os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename) )
BUCKET_NAME = [span class="hljs-string">'fsample123']
result = upload_blob(BUCKET_NAME, TMP_FILE_NAME, f "tmp/{secure_filename(f.filename)}"/span>)
#End Do Main Process Here。
return result
except Exception as e:
return str(e)。
def upload_blob(bucket_name, source_file_name, destination_blob_name) 。
""上傳一個檔案到bucket。""。
# SOURCE:/span>
# https://cloud.google.com/storage/docs/uploading-objects#uploading-an-object
# The ID of your GCS bucket # The ID of your GCS bucket
# bucket_name = "your-bucket-name"/span>
# 要上傳的檔案路徑 # bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# 你的GCS物件的ID # 你的GCS物件的ID
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob( destination_blob_name)
blob.upload_from_filename(source_file_name)
return "File {} uploaded to {}.".format(Source_file_name, destination_blob_name)
if __name__ == '__main__'/span>。
app.run()
uj5u.com熱心網友回復:
因此,問題不在于上傳到GCS,而在于你創建的臨時檔案。
唯一有寫權限的目錄是/tmp,要注意它在GAE實體之間不共享,當實體重新啟動時它會消失...
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name) 。
""上傳一個檔案到bucket。""。
# SOURCE:/span>
# https://cloud.google.com/storage/docs/uploading-objects#uploading-an-object
# The ID of your GCS bucket # The ID of your GCS bucket
# bucket_name = "your-bucket-name"/span>
# 要上傳的檔案路徑 # bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# 你的GCS物件的ID # 你的GCS物件的ID
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob( destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"檔案{}上傳至{}。".format(
source_file_name, destination_blob_name
)
)
TMP_PATH = "/tmp/"。
TMP_FILE_NAME = f"{TMP_PATH}file.xlsx"
BUCKET_NAME = '<your-bucket-name>'/span>
with open(TMP_FILE_NAME, "w") as outfile:
outfile.write("write file action goes here")
upload_blob(BUCKET_NAME, TMP_FILE_NAME, "目標檔案路徑在這里...")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/317554.html
標籤:
