雲端上傳Cloud Upload

前言大綱

cloud_upload.py 腳本顯示如何將文件上傳到 Google Cloud Platform (GCP) 存儲桶或 Amazon Web Services (AWS) S3 存儲桶。

在 cloud_upload.py 腳本中定義了以下兩個函數:

def upload_to_gcp(bucket_name, source_file, destination_file):
def upload_to_aws(bucket_name, source_file, destination_file):

他們都採用相同的參數:

        bucket_name = "your-bucket-name"
        source_file = "local/path/to/file"
        destination_file = "storage-object-name"

憑據存儲在文件中。 對於 GCP,用戶需要設置一個環境變量; 對於 AWS,憑證是硬編碼的。 詳情請見下文。

安裝套件

需要安裝兩個 python 套件:

  • google-cloud-storage
  • boto3

python3 -m pip install -r requirements.txt

證書 Credentials

每個雲服務都需要正確設置證書。

GCP

對於 GCP,用戶需要使用環境變量指向服務帳戶 .json 文件。以下是如何使用 linux 執行此操作的示例:
export GOOGLE_APPLICATION_CREDENTIALS=<path-to-file>/<filename>.json

將以上行添加到您的 .bashrc 文件中以使其永久化。可以 在此處  找到更多信息(包括 Windows 示例)。

AWS

對於 AWS,用戶必須將默認用戶憑證文件存儲在 ~/.aws/credentials 中。更多信息可以在 這裡 找到。

運行示例

如果雲憑證已正確設置,則 cloud_upload.py 中的 main() 函數當前配置為將 requirements.txt 上傳到 AWS S3 存儲桶(只需將 upload_to_aws 更改為 upload_to_gcp 即可使用 GCP 存儲桶)。下麵包含了 cloud_upload.py 的一個片段。
aws_bucket_name = 'aws-imagedemo' # s3 bucket name
source_file = 'requirements.txt' # sample file to upload
destination_file = source_file # using source_file name as destination_file name
upload_to_aws(aws_bucket_name, source_file, destination_file)

通過在 CLI 中執行此命令來運行示例腳本。

python3 cloud_upload.py

對於成功的 AWS 上傳,用戶應該看到以下響應。
 Found credentials in shared credentials file: ~/.aws/credentials (credentials.py:1196)
Upload of file requirements.txt as requirements.txt to aws-imagedemo successful (cloud_upload.py:45)

這是成功上傳 GCP 的響應。

Upload of file requirements.txt as requirements.txt to c-imagedemo successful (cloud_upload.py:26)

自動行走示例

可以在 ricoh_theta 示例中找到如何在 remote_mission_service 應用程序中使用這些函數的示例。


參考資料

特色、摘要,Feature、Summary:

關鍵字、標籤,Keyword、Tag:

留言

這個網誌中的熱門文章

Ubuntu 常用指令、分類與簡介

iptables的觀念與使用

網路設定必要參數IP、netmask(遮罩)、Gateway(閘道)、DNS

了解、分析登錄檔 - log

Python 與SQLite 資料庫

Blogger文章排版範本

Pandas 模組

如何撰寫Shell Script

查詢指令或設定 -Linux 線上手冊 - man

下載網頁使用 requests 模組