Escrever vários objetos em um único objeto em um bucket do Cloud Storage.
Mais informações
Para ver a documentação detalhada que inclui este exemplo de código, consulte:
Exemplo de código
C#
Para mais informações, consulte a documentação de referência da API Cloud Storage para C#.
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;
using System.Collections.Generic;
public class ComposeObjectSample
{
public void ComposeObject(
string bucketName = "your-bucket-name",
string firstObjectName = "your-first-object-name",
string secondObjectName = "your-second-object-name",
string targetObjectName = "new-composite-object-name")
{
var storage = StorageClient.Create();
var sourceObjects = new List<ComposeRequest.SourceObjectsData>
{
new ComposeRequest.SourceObjectsData { Name = firstObjectName },
new ComposeRequest.SourceObjectsData { Name = secondObjectName }
};
//You could add as many sourceObjects as you want here, up to the max of 32.
storage.Service.Objects.Compose(new ComposeRequest
{
SourceObjects = sourceObjects,
Destination = new Google.Apis.Storage.v1.Data.Object { ContentType = "text/plain" }
}, bucketName, targetObjectName).Execute();
Console.WriteLine($"New composite file {targetObjectName} was created in bucket {bucketName}" +
$" by combining {firstObjectName} and {secondObjectName}.");
}
}
C++
Para mais informações, consulte a documentação de referência da API Cloud Storage para C++.
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
Para mais informações, consulte a documentação de referência da API Cloud Storage para Go.
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
}
Java
Para mais informações, consulte a documentação de referência da API Cloud Storage para Java.
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
Para mais informações, consulte a documentação de referência da API Cloud Storage para Node.js.
/**
* 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);
PHP
Para mais informações, consulte a documentação de referência da API Cloud Storage para PHP.
use Google\Cloud\Storage\StorageClient;
/**
* Compose two objects into a single target object.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* @param string $firstObjectName The name of the first GCS object to compose.
* @param string $secondObjectName The name of the second GCS object to compose.
* @param string $targetObjectName The name of the object to be created.
*/
function compose_file($bucketName, $firstObjectName, $secondObjectName, $targetObjectName)
{
// $bucketName = 'my-bucket';
// $firstObjectName = 'my-object-1';
// $secondObjectName = 'my-object-2';
// $targetObjectName = 'composed-my-object-1-my-object-2';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
// In this example, we are composing only two objects, but Cloud Storage supports
// composition of up to 32 objects.
$objectsToCompose = [$firstObjectName, $secondObjectName];
$targetObject = $bucket->compose($objectsToCompose, $targetObjectName, [
'destination' => [
'contentType' => 'application/octet-stream'
]
]);
if ($targetObject->exists()) {
printf(
'New composite object %s was created by combining %s and %s',
$targetObject->name(),
$firstObjectName,
$secondObjectName
);
}
}
Python
Para mais informações, consulte a documentação de referência da API Cloud Storage para Python.
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
Para mais informações, consulte a documentação de referência da API Cloud Storage para Ruby.
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, skip_lookup: true
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
A seguir
Para pesquisar e filtrar exemplos de código de outros produtos do Google Cloud, consulte o navegador de exemplos do Google Cloud.