Renommer des objets de manière atomique
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Renommez des objets de manière atomique dans un bucket. Pour renommer un objet, vous pouvez utiliser la méthode Objects: move.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["# Rename objects atomically within a bucket. To rename an object, you can use the Objects: move method.\n\nCode sample\n-----------\n\n### C#\n\n\nFor more information, see the\n[Cloud Storage C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;\n using System;\n\n public class MoveObjectSample\n {\n /// \u003csummary\u003e\n /// Moves an object within a bucket with hierarchical namespace enabled.\n /// \u003c/summary\u003e\n /// \u003cparam name=\"sourceBucketName\"\u003eName of the source bucket containing the object to move.\u003c/param\u003e\n /// \u003cparam name=\"sourceObjectName\"\u003eThe name of the source object to move within the bucket.\u003c/param\u003e\n /// \u003cparam name=\"destinationObjectName\"\u003eThe name of the new object to move to within the bucket.\u003c/param\u003e\n public void MoveObject(\n string sourceBucketName = \"source-bucket-name\",\n string sourceObjectName = \"source-object-name\",\n string destinationObjectName = \"destination-object-name\")\n {\n var storage = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();\n storage.MoveObject(sourceBucketName, sourceObjectName, destinationObjectName);\n Console.WriteLine($\"Moved {sourceBucketName}/{sourceObjectName} to \" + $\"{sourceBucketName}/{destinationObjectName} within a hierarchical namespace enabled bucket.\");\n }\n }\n\n### Node.js\n\n\nFor more information, see the\n[Cloud Storage Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // The ID of your GCS bucket\n // const bucketName = 'your-source-bucket';\n\n // The ID of your GCS file\n // const srcFileName = 'your-file-name';\n\n // The new ID for your GCS file\n // const destFileName = 'your-new-file-name';\n\n // Imports the Google Cloud client library\n const {Storage} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n async function moveFileAtomic() {\n // Optional:\n // Set a generation-match precondition to avoid potential race conditions\n // and data corruptions. The request to copy is aborted if the object's\n // generation number does not match your precondition. For a destination\n // object that does not yet exist, set the ifGenerationMatch precondition to 0\n // If the destination object already exists in your bucket, set instead a\n // generation-match precondition using its generation number.\n const moveOptions = {\n preconditionOpts: {\n ifGenerationMatch: destinationGenerationMatchPrecondition,\n },\n };\n\n // Moves the file automatically within the HNS enabled bucket\n await storage\n .bucket(bucketName)\n .file(srcFileName)\n .https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/file_2.html(destFileName, moveOptions);\n\n console.log(\n `gs://${bucketName}/${srcFileName} moved to gs://${bucketName}/${destFileName}`\n );\n }\n\n moveFileAtomic().catch(console.error);\n\n### PHP\n\n\nFor more information, see the\n[Cloud Storage PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n use Google\\Cloud\\Storage\\StorageClient;\n\n /**\n * Move an object to a new name within HNS-enabled bucket.\n *\n * @param string $bucketName The name of your Cloud Storage bucket.\n * (e.g. 'my-bucket')\n * @param string $objectName The name of your Cloud Storage object.\n * (e.g. 'my-object')\n * @param string $newObjectName the destination object name.\n * (e.g. 'my-other-object')\n */\n function move_object_atomic(string $bucketName, string $objectName, string $newObjectName): void\n {\n $storage = new StorageClient();\n $bucket = $storage-\u003ebucket($bucketName);\n $object = $bucket-\u003eobject($objectName);\n $object-\u003emove($newObjectName);\n printf('Moved gs://%s/%s to gs://%s/%s' . PHP_EOL,\n $bucketName,\n $objectName,\n $bucketName,\n $newObjectName);\n }\n\n### Python\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/storage/latest/\n\n\n def move_object(bucket_name: str, blob_name: str, new_blob_name: str) -\u003e None:\n \"\"\"Moves a blob to a new name within the same bucket using the move API.\"\"\"\n # The name of your GCS bucket\n # bucket_name = \"your-bucket-name\"\n\n # The name of your GCS object to move\n # blob_name = \"your-file-name\"\n\n # The new name of the GCS object\n # new_blob_name = \"new-file-name\"\n\n storage_client = https://cloud.google.com/python/docs/reference/storage/latest/.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()\n\n bucket = storage_client.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)\n blob_to_move = bucket.blob(blob_name)\n\n # Use move_blob to perform an efficient, server-side move.\n moved_blob = bucket.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_move_blob(\n blob=blob_to_move, new_name=new_blob_name\n )\n\n print(f\"Blob {blob_to_move.name} has been moved to {moved_blob.name}.\")\n\n### Ruby\n\n\nFor more information, see the\n[Cloud Storage Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n def move_object bucket_name:, source_file_name:, destination_file_name:\n # The ID of your GCS bucket\n # bucket_name = \"your-unique-bucket-name\"\n\n # The name of your GCS object\n # source_file_name = \"your-file-name\"\n\n # The new object name which you want to craete\n # destination_file_name = \"your-new-file-name\"\n\n require \"google/cloud/storage\"\n\n storage = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-storage-control-v2/latest/Google-Cloud-Storage.html.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html\n bucket = storage.bucket bucket_name, skip_lookup: true\n\n bucket.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html source_file_name, destination_file_name\n fetch_file = bucket.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html destination_file_name\n puts \"New File #{fetch_file.name} created\\n\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]