스토리지 일괄 생성 작업

스토리지 일괄 작업 만들기

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

C++

자세한 내용은 Cloud Storage C++ API 참조 문서를 확인하세요.

Cloud Storage에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 클라이언트 라이브러리의 인증 설정을 참조하세요.

[](google::cloud::storagebatchoperations_v1::StorageBatchOperationsClient
       client,
   std::string const& project_id, std::string const& job_id,
   std::string const& target_bucket_name, std::string const& object_prefix) {
  auto const parent =
      std::string{"projects/"} + project_id + "/locations/global";
  namespace sbo = google::cloud::storagebatchoperations::v1;
  sbo::Job job;
  sbo::BucketList* bucket_list = job.mutable_bucket_list();
  sbo::BucketList::Bucket* bucket_config = bucket_list->add_buckets();
  bucket_config->set_bucket(target_bucket_name);
  sbo::PrefixList* prefix_list_config = bucket_config->mutable_prefix_list();
  prefix_list_config->add_included_object_prefixes(object_prefix);
  sbo::DeleteObject* delete_object_config = job.mutable_delete_object();
  delete_object_config->set_permanent_object_deletion_enabled(false);
  auto result = client.CreateJob(parent, job, job_id).get();
  if (!result) throw result.status();
  std::cout << "Created job: " << result->name() << "\n";
}

PHP

자세한 내용은 Cloud Storage PHP API 참조 문서를 확인하세요.

Cloud Storage에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 클라이언트 라이브러리의 인증 설정을 참조하세요.

use Google\Cloud\StorageBatchOperations\V1\Client\StorageBatchOperationsClient;
use Google\Cloud\StorageBatchOperations\V1\CreateJobRequest;
use Google\Cloud\StorageBatchOperations\V1\Job;
use Google\Cloud\StorageBatchOperations\V1\BucketList;
use Google\Cloud\StorageBatchOperations\V1\BucketList\Bucket;
use Google\Cloud\StorageBatchOperations\V1\PrefixList;
use Google\Cloud\StorageBatchOperations\V1\DeleteObject;

/**
 * Create a new batch job.
 *
 * @param string $projectId Your Google Cloud project ID.
 *        (e.g. 'my-project-id')
 * @param string $jobId A unique identifier for this job.
 *        (e.g. '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5')
 * @param string $bucketName The name of your Cloud Storage bucket to operate on.
 *        (e.g. 'my-bucket')
 * @param string $objectPrefix The prefix of objects to include in the operation.
 *        (e.g. 'prefix1')
 */
function create_job(string $projectId, string $jobId, string $bucketName, string $objectPrefix): void
{
    // Create a client.
    $storageBatchOperationsClient = new StorageBatchOperationsClient();

    $parent = $storageBatchOperationsClient->locationName($projectId, 'global');

    $prefixListConfig = new PrefixList(['included_object_prefixes' => [$objectPrefix]]);
    $bucket = new Bucket(['bucket' => $bucketName, 'prefix_list' => $prefixListConfig]);
    $bucketList = new BucketList(['buckets' => [$bucket]]);

    $deleteObject = new DeleteObject(['permanent_object_deletion_enabled' => false]);

    $job = new Job(['bucket_list' => $bucketList, 'delete_object' => $deleteObject]);

    $request = new CreateJobRequest([
        'parent' => $parent,
        'job_id' => $jobId,
        'job' => $job,
    ]);
    $response = $storageBatchOperationsClient->createJob($request);

    printf('Created job: %s', $response->getName());
}

Ruby

자세한 내용은 Cloud Storage Ruby API 참조 문서를 확인하세요.

Cloud Storage에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 클라이언트 라이브러리의 인증 설정을 참조하세요.

require "google/cloud/storage_batch_operations"

# Creates a Storage Batch Operations job to delete objects in a bucket
# that match a given prefix. The deletion is a "soft delete", meaning
# objects can be recovered if versioning is enabled on the bucket.
#
# @param bucket_name [String] The name of the Google Cloud Storage bucket.
# @param prefix [String] The prefix of the objects to be included in the job.
#   The job will operate on all objects whose names start with this prefix.
# @param job_id [String] A unique identifier for the job.
# @param project_id [String] The ID of the Google Cloud project where the job will be created.
#
# @example
#   create_job(
#     bucket_name: "your-unique-bucket-name",
#     prefix: "test-files/",
#     job_id: "your-job-id",
#     project_id: "your-project-id"
#   )
#
def create_job bucket_name:, prefix:, job_id:, project_id:
  client = Google::Cloud::StorageBatchOperations.storage_batch_operations

  parent = "projects/#{project_id}/locations/global"

  prefix_list = Google::Cloud::StorageBatchOperations::V1::PrefixList.new(
    included_object_prefixes: [prefix]
  )

  bucket = Google::Cloud::StorageBatchOperations::V1::BucketList::Bucket.new(
    bucket: bucket_name,
    prefix_list: prefix_list
  )

  bucket_list = Google::Cloud::StorageBatchOperations::V1::BucketList.new(
    buckets: [bucket]
  )

  # Define the delete operation
  delete_object = Google::Cloud::StorageBatchOperations::V1::DeleteObject.new(
    permanent_object_deletion_enabled: false
  )

  # Build the job
  job = Google::Cloud::StorageBatchOperations::V1::Job.new(
    bucket_list: bucket_list,
    delete_object: delete_object
  )

  request = Google::Cloud::StorageBatchOperations::V1::CreateJobRequest.new parent: parent, job_id: job_id, job: job
  create_job_operation = client.create_job request

  # To fetch job details using get_job to confirm creation
  get_request = Google::Cloud::StorageBatchOperations::V1::GetJobRequest.new name: "#{parent}/jobs/#{job_id}"

  begin
    ## Waiting for operation to complete
    create_job_operation.wait_until_done!
    ## Fetch the newly created job to confirm creation
    job_detail = client.get_job get_request
    message = "Storage Batch Operations job #{job_detail.name} is created."
  rescue StandardError
    # This error is thrown when the job is not created.
    message = "Failed to create job #{job_id}. Error: #{create_job_operation.error.message}"
  end
  puts message
end

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기