Cloud Storage 버킷의 여러 객체를 단일 객체로 작성합니다.
이 코드 샘플이 포함된 문서 페이지
컨텍스트에서 사용된 코드 샘플을 보려면 다음 문서를 참조하세요.
코드 샘플
C++
자세한 내용은 Cloud Storage C++ API 참조 문서를 확인하세요.
namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name,
std::string const& destination_object_name,
std::vector<gcs::ComposeSourceObject> const& compose_objects) {
StatusOr<gcs::ObjectMetadata> composed_object = client.ComposeObject(
bucket_name, compose_objects, destination_object_name);
if (!composed_object) {
throw std::runtime_error(composed_object.status().message());
}
std::cout << "Composed new object " << composed_object->name()
<< " in bucket " << composed_object->bucket()
<< "\nFull metadata: " << *composed_object << "\n";
}
Go
자세한 내용은 Cloud Storage Go API 참조 문서를 확인하세요.
import (
"context"
"fmt"
"io"
"time"
"cloud.google.com/go/storage"
)
// composeFile composes source objects to create a composite object.
func composeFile(w io.Writer, bucket, object1, object2, toObject string) error {
// bucket := "bucket-name"
// object1 := "object-name-1"
// object2 := "object-name-2"
// toObject := "object-name-3"
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
src1 := client.Bucket(bucket).Object(object1)
src2 := client.Bucket(bucket).Object(object2)
dst := client.Bucket(bucket).Object(toObject)
// ComposerFrom takes varargs, so you can put as many objects here
// as you want.
_, err = dst.ComposerFrom(src1, src2).Run(ctx)
if err != nil {
return fmt.Errorf("ComposerFrom: %v", err)
}
fmt.Fprintf(w, "New composite object %v was created by combining %v and %v\n", toObject, object1, object2)
return nil
}
자바
자세한 내용은 Cloud Storage 자바 API 참조 문서를 확인하세요.
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class ComposeObject {
public static void composeObject(
String bucketName,
String firstObjectName,
String secondObjectName,
String targetObjectName,
String projectId) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of the first GCS object to compose
// String firstObjectName = "your-first-object-name";
// The ID of the second GCS object to compose
// String secondObjectName = "your-second-object-name";
// The ID to give the new composite object
// String targetObjectName = "new-composite-object-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Storage.ComposeRequest composeRequest =
Storage.ComposeRequest.newBuilder()
// addSource takes varargs, so you can put as many objects here as you want, up to the
// max of 32
.addSource(firstObjectName, secondObjectName)
.setTarget(BlobInfo.newBuilder(bucketName, targetObjectName).build())
.build();
Blob compositeObject = storage.compose(composeRequest);
System.out.println(
"New composite object "
+ compositeObject.getName()
+ " was created by combining "
+ firstObjectName
+ " and "
+ secondObjectName);
}
}
Node.js
자세한 내용은 Cloud Storage Node.js API 참조 문서를 확인하세요.
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';
// The ID of the first GCS file to compose
// const firstFileName = 'your-first-file-name';
// The ID of the second GCS file to compose
// const secondFileName = 'your-second-file-name';
// The ID to give the new composite file
// const destinationFileName = 'new-composite-file-name';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function composeFile() {
const bucket = storage.bucket(bucketName);
const sources = [firstFileName, secondFileName];
await bucket.combine(sources, destinationFileName);
console.log(
`New composite file ${destinationFileName} was created by combining ${firstFileName} and ${secondFileName}`
);
}
composeFile().catch(console.error);
Python
자세한 내용은 Cloud Storage Python API 참조 문서를 확인하세요.
from google.cloud import storage
def compose_file(bucket_name, first_blob_name, second_blob_name, destination_blob_name):
"""Concatenate source blobs into destination blob."""
# bucket_name = "your-bucket-name"
# first_blob_name = "first-object-name"
# second_blob_name = "second-blob-name"
# destination_blob_name = "destination-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
destination = bucket.blob(destination_blob_name)
destination.content_type = "text/plain"
# sources is a list of Blob instances, up to the max of 32 instances per request
sources = [bucket.get_blob(first_blob_name), bucket.get_blob(second_blob_name)]
destination.compose(sources)
print(
"New composite object {} in the bucket {} was created by combining {} and {}".format(
destination_blob_name, bucket_name, first_blob_name, second_blob_name
)
)
return destination
Ruby
자세한 내용은 Cloud Storage Ruby API 참조 문서를 확인하세요.
def compose_file bucket_name:, first_file_name:, second_file_name:, destination_file_name:
# The ID of your GCS bucket
# bucket_name = "your-unique-bucket-name"
# The ID of the first GCS object to compose
# first_file_name = "your-first-file-name"
# The ID of the second GCS object to compose
# second_file_name = "your-second-file-name"
# The ID to give the new composite object
# destination_file_name = "new-composite-file-name"
require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket bucket_name
destination = bucket.compose [first_file_name, second_file_name], destination_file_name do |f|
f.content_type = "text/plain"
end
puts "Composed new file #{destination.name} in the bucket #{bucket_name} " \
"by combining #{first_file_name} and #{second_file_name}"
end
다음 단계
다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.