下載物件

本頁面說明如何從 Cloud Storage 的值區下載物件。如需物件總覽,請參閱重要詞彙

Console

  1. 在 Google Cloud Platform Console 中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找出包含要下載之物件的值區,然後按一下該值區的名稱,並前往該物件的所在位置。

  3. 按一下與物件相關的下拉式選單。

    下拉式選單可透過畫面最右側的三個垂直排列圓點存取。

  4. 選取 [Download] (下載)

gsutil

使用 gsutil cp 指令:

gsutil cp gs://[BUCKET_NAME]/[OBJECT_NAME] [SAVE_TO_LOCATION]

其中:

  • [BUCKET_NAME] 是包含要下載之物件的值區名稱。例如:my-bucket

  • [OBJECT_NAME] 是您要下載的物件名稱。例如:pets/dog.png

  • [SAVE_TO_LOCATION] 是您儲存物件的本機路徑。例如:Desktop/Images

如果成功,回應內容會與以下範例類似:

Operation completed over 1 objects/58.8 KiB.

程式碼範例

C++

詳情請參閱 Cloud Storage C++ API 參考資料說明文件

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  gcs::ObjectReadStream stream = client.ReadObject(bucket_name, object_name);

  int count = 0;
  std::string line;
  while (std::getline(stream, line, '\n')) {
    ++count;
  }

  std::cout << "The object has " << count << " lines\n";
}

C#

詳情請參閱 Cloud Storage C# API 參考資料說明文件

private void DownloadObject(string bucketName, string objectName,
    string localPath = null)
{
    var storage = StorageClient.Create();
    localPath = localPath ?? Path.GetFileName(objectName);
    using (var outputFile = File.OpenWrite(localPath))
    {
        storage.DownloadObject(bucketName, objectName, outputFile);
    }
    Console.WriteLine($"downloaded {objectName} to {localPath}.");
}

Go

詳情請參閱 Cloud Storage Go API 參考資料說明文件

rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
if err != nil {
	return nil, err
}
defer rc.Close()

data, err := ioutil.ReadAll(rc)
if err != nil {
	return nil, err
}
return data, nil

Java

詳情請參閱 Cloud Storage Java API 參考資料說明文件

// The name of the bucket to access
// String bucketName = "my-bucket";

// The name of the remote file to download
// String srcFilename = "file.txt";

// The path to which the file should be downloaded
// Path destFilePath = Paths.get("/local/path/to/file.txt");

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// Get specific file from specified bucket
Blob blob = storage.get(BlobId.of(bucketName, srcFilename));

// Download file to specified path
blob.downloadTo(destFilePath);

Node.js

詳情請參閱 Cloud Storage Node.js API 參考資料說明文件

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'Remote file to download, e.g. file.txt';
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';

const options = {
  // The path to which the file should be downloaded, e.g. "./file.txt"
  destination: destFilename,
};

// Downloads the file
await storage
  .bucket(bucketName)
  .file(srcFilename)
  .download(options);

console.log(
  `gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);

PHP

詳情請參閱 Cloud Storage PHP API 參考資料說明文件

use Google\Cloud\Storage\StorageClient;

/**
 * Download an object from Cloud Storage and save it as a local file.
 *
 * @param string $bucketName the name of your Google Cloud bucket.
 * @param string $objectName the name of your Google Cloud object.
 * @param string $destination the local destination to save the encrypted object.
 *
 * @return void
 */
function download_object($bucketName, $objectName, $destination)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->downloadToFile($destination);
    printf('Downloaded gs://%s/%s to %s' . PHP_EOL,
        $bucketName, $objectName, basename($destination));
}

Python

詳情請參閱 Cloud Storage Python API 參考資料說明文件

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)

    blob.download_to_filename(destination_file_name)

    print('Blob {} downloaded to {}.'.format(
        source_blob_name,
        destination_file_name))

Ruby

詳情請參閱 Cloud Storage Ruby API 參考資料說明文件

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of file in Google Cloud Storage to download locally"
# local_path  = "Destination path for downloaded file"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.download local_path

puts "Downloaded #{file.name}"

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 使用 cURL 透過 GET 物件要求呼叫 JSON API

    curl -X GET \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      -o "[SAVE_TO_LOCATION]" \
      "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]?alt=media"

    其中:

    • [OAUTH2_TOKEN] 是您在步驟 1 中產生的存取憑證。
    • [SAVE_TO_LOCATION] 是您儲存物件的本機路徑。例如:Desktop/Images
    • [BUCKET_NAME] 是包含要下載物件的值區名稱。例如:my-bucket
    • [OBJECT_NAME] 是您要下載的物件名稱。例如:pets/dog.png。如要瞭解如何對物件名稱進行網址編碼,使其路徑可安全使用,請參閱對 URI 路徑部分進行編碼一節。

XML API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 使用 cURL 透過 GET 物件要求呼叫 XML API

    curl -X GET \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      -o "[SAVE_TO_LOCATION]" \
      "https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]"

    其中:

    • [OAUTH2_TOKEN] 是您在步驟 1 中產生的存取憑證。
    • [SAVE_TO_LOCATION] 是您儲存物件的本機路徑。例如:Desktop/Images
    • [BUCKET_NAME] 是包含要下載物件的值區名稱。例如:my-bucket
    • [OBJECT_NAME] 是您要下載的物件名稱。例如:pets/dog.png

如要輕鬆下載值區或子目錄中的所有物件,請使用 gsutil cp 指令。

後續步驟

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Storage
需要協助嗎?請前往我們的支援網頁