【 Cloud 】透過 Python 上傳與複製檔案到 Azure Blob

【 Cloud 】透過 Python 上傳與複製檔案到 Azure Blob
Photo by Jake Irish / Unsplash

內容

  • 學習目標
  • 前置準備作業
  • 取得 Storage accounts 的 Connection string
  • 將本地端電腦中的檔案上傳到 Azure Storage accounts
  • 將 Byte Image 檔案直接上傳到 Azure Blob
  • 透過檔案的 URL 來上傳到 Azure Blob
  • 取的來源端 Container 中檔案的 URL 並透過此 URL 上傳到目地端的 Container
  • 掃描 Azure Blob 來源端特定的資料夾,並依來源端的檔案結構上傳到目地端的 Container

前置準備作業

取得 Storage accounts 的 Connection string

Step 1. 點選 Access keys > 點選 Connection string 右邊的 Show 即可取得 Connection string

---2023-08-23---10.11.03


Step 2. 於本地端電腦中的程式資料夾建立 .env 檔案

  • 請於 .env 檔案中輸入以下資訊

將本地端電腦中的檔案上傳到 Azure Storage accounts

Step 1. 本地端電腦中的檔案結構

  • 將本地端電腦中 Datasets 資料夾中的檔案並依 Datasets 資料夾中的檔案結構上傳到 Azure Blob
.
├── .env
├── app.py
└── Datasets
    ├── 6.txt
    ├── 7.txt
    ├── folder-1
    │   ├── 1.txt
    │   ├── 2.txt
    │   └── folder-3
    │       ├── 8.txt
    │       └── 9.txt
    └── folder-2
        ├── 3.txt
        └── 5.txt

Step 2. 撰寫上傳程式

  • 建立 app.py 檔案並輸入下方程式
    • 修改第 7 行成要上傳到 Azure Blob 中的哪個 Container

Step 3. 執行程式

  • 請於終端機輸入下方指令
python app.py

---2023-08-23---10.42.30


Step 4. 查看 Azure Blob 上傳結果

.
└── files
    ├── 6.txt
    ├── 7.txt
    ├── folder-1
    │   ├── 1.txt
    │   ├── 2.txt
    │   └── folder-3
    │       ├── 8.txt
    │       └── 9.txt
    └── folder-2
        ├── 3.txt
        └── 5.txt

---2023-08-23---10.50.42

---2023-08-23---10.52.06

---2023-08-23---10.52.13

---2023-08-23---10.52.23

將 Byte Image 檔案直接上傳到 Azure Blob

Step 1. 撰寫上傳程式

  • 建立 app.py 檔案並輸入下方程式
    • 修改第 17 行成要上傳到 Azure Blob 中的哪個 Container

Step 2. 執行程式

  • 請於終端機輸入下方指令
python app.py

---2023-08-23---1.31.11


Step 3. 查看 Azure Blob 上傳結果

---2023-08-23---1.31.44

透過檔案的 URL 來上傳到 Azure Blob

Step 1. 撰寫上傳程式

  • 建立 app.py 檔案並輸入下方程式
    • 修改第 8 行成要上傳到 Azure Blob 中的哪個 Container
    • 修改第 9 行成上傳到 Azure Blob 後的檔案名稱
    • 修改第 15 行成取得來源檔案的 URL

Step 2. 執行程式

  • 請於終端機輸入下方指令
python app.py

---2023-08-23---1.37.13


Step 3. 查看 Azure Blob 上傳結果

---2023-08-23---1.31.44

取的來源端 Container 中檔案的 URL 並透過此 URL 上傳到目地端的 Container

Step 1. Azure Blob 的 來源目的地的 Container 之檔案結構

.
└── source
│   ├── 6.txt
│   ├── 7.txt
│   ├── folder-1
│   │   ├── 1.txt
│   │   ├── 2.txt
│   │   └── folder-3
│   │       ├── 8.txt
│   │       └── 9.txt
│   └── folder-2
│       ├── 3.txt
│       └── 5.txt
└── destination

Step 2. 撰寫上傳程式

  • 建立 app.py 檔案並輸入下方程式
    • 修改第 9 行成來源端 Container 名稱
    • 修改第 10 行成目地端 Container 名稱

Step 3. 執行程式

  • 請於終端機輸入下方指令
python app.py

---2023-08-23---1.55.20


Step 4. 查看 Azure Blob 上傳結果

.
└── source
│   ├── 6.txt
│   ├── 7.txt
│   ├── folder-1
│   │   ├── 1.txt
│   │   ├── 2.txt
│   │   └── folder-3
│   │       ├── 8.txt
│   │       └── 9.txt
│   └── folder-2
│       ├── 3.txt
│       └── 5.txt
└── destination
    ├── 6.txt
    ├── 7.txt
    ├── folder-1
    │   ├── 1.txt
    │   ├── 2.txt
    │   └── folder-3
    │       ├── 8.txt
    │       └── 9.txt
    └── folder-2
        ├── 3.txt
        └── 5.txt

---2023-08-23---1.55.50

掃描 Azure Blob 來源端特定的資料夾,並依來源端的檔案結構上傳到目地端的 Container

Step 1. Azure Blob 的 來源目的地的 Container 之檔案結構

.
└── source
│   ├── 6.txt
│   ├── 7.txt
│   ├── folder-1
│   │   ├── 1.txt
│   │   ├── 2.txt
│   │   └── folder-3
│   │       ├── 8.txt
│   │       └── 9.txt
│   └── folder-2
│       ├── 3.txt
│       └── 5.txt
└── destination

Step 2. 撰寫上傳程式

  • 建立 app.py 檔案並輸入下方程式
    • 修改第 10 行成來源端 Container 名稱
    • 修改第 11 行成目地端 Container 名稱
    • 修改第 12 行成開始掃描的資料夾名稱,本範例傳送 folder-1/folder-3 資料夾中的檔案

Step 3. 執行程式

  • 請於終端機輸入下方指令
python app.py

---2023-08-23---2.02.29


Step 4. 查看 Azure Blob 上傳結果

.
└── source
│   ├── 6.txt
│   ├── 7.txt
│   ├── folder-1
│   │   ├── 1.txt
│   │   ├── 2.txt
│   │   └── folder-3
│   │       ├── 8.txt
│   │       └── 9.txt
│   └── folder-2
│       ├── 3.txt
│       └── 5.txt
└── destination
    └── folder-1
        └── folder-3
            ├── 8.txt
            └── 9.txt

---2023-08-23---2.02.55

GitHub

List of blogs