Importer de nombreux objets
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Utilisez Transfer Manager pour importer de nombreux fichiers en profitant de la simultanéité.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les pages suivantes :
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,["# Upload many objects\n\nUse Transfer Manager to upload many files with concurrency.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Host a static website](/storage/docs/hosting-static-website)\n- [Hosting a static website using HTTP](/storage/docs/hosting-static-website-http)\n- [Upload objects from a file system](/storage/docs/uploading-objects)\n\nCode sample\n-----------\n\n### Java\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\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 import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelUploadConfig.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.UploadResult.html;\n import java.io.IOException;\n import java.nio.file.Path;\n import java.util.List;\n\n class UploadMany {\n\n public static void uploadManyFiles(String bucketName, List\u003cPath\u003e files) throws IOException {\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html transferManager = https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html.newBuilder().build().https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelUploadConfig.html parallelUploadConfig =\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelUploadConfig.html.newBuilder().setBucketName(bucketName).build();\n List\u003cUploadResult\u003e results =\n transferManager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html#com_google_cloud_storage_transfermanager_TransferManager_uploadFiles_java_util_List_java_nio_file_Path__com_google_cloud_storage_transfermanager_ParallelUploadConfig_(files, parallelUploadConfig).https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.UploadJob.html#com_google_cloud_storage_transfermanager_UploadJob_getUploadResults__();\n for (https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.UploadResult.html result : results) {\n System.out.println(\n \"Upload for \"\n + result.getInput().getName()\n + \" completed with status \"\n + result.getStatus());\n }\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-unique-bucket-name';\n\n // The ID of the first GCS file to upload\n // const firstFilePath = 'your-first-file-name';\n\n // The ID of the second GCS file to upload\n // const secondFilePath = 'your-second-file-name';\n\n // Imports the Google Cloud client library\n const {Storage, TransferManager} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n // Creates a transfer manager client\n const transferManager = new https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/transfermanager.html(storage.bucket(bucketName));\n\n async function uploadManyFilesWithTransferManager() {\n // Uploads the files\n await transferManager.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/transfermanager.html([firstFilePath, secondFilePath]);\n\n for (const filePath of [firstFilePath, secondFilePath]) {\n console.log(`${filePath} uploaded to ${bucketName}.`);\n }\n }\n\n uploadManyFilesWithTransferManager().catch(console.error);\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 def upload_many_blobs_with_transfer_manager(\n bucket_name, filenames, source_directory=\"\", workers=8\n ):\n \"\"\"Upload every file in a list to a bucket, concurrently in a process pool.\n\n Each blob name is derived from the filename, not including the\n `source_directory` parameter. For complete control of the blob name for each\n file (and other aspects of individual blob metadata), use\n transfer_manager.upload_many() instead.\n \"\"\"\n\n # The ID of your GCS bucket\n # bucket_name = \"your-bucket-name\"\n\n # A list (or other iterable) of filenames to upload.\n # filenames = [\"file_1.txt\", \"file_2.txt\"]\n\n # The directory on your computer that is the root of all of the files in the\n # list of filenames. This string is prepended (with os.path.join()) to each\n # filename to get the full path to the file. Relative paths and absolute\n # paths are both accepted. This string is not included in the name of the\n # uploaded blob; it is only used to find the source files. An empty string\n # means \"the current working directory\". Note that this parameter allows\n # directory traversal (e.g. \"/\", \"../\") and is not intended for unsanitized\n # end user input.\n # source_directory=\"\"\n\n # The maximum number of processes to use for the operation. The performance\n # impact of this value depends on the use case, but smaller files usually\n # benefit from a higher number of processes. Each additional process occupies\n # some CPU and memory resources until finished. Threads can be used instead\n # of processes by passing `worker_type=transfer_manager.THREAD`.\n # workers=8\n\n from google.cloud.storage import https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html, https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html\n\n storage_client = Client()\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\n results = https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html(\n bucket, filenames, source_directory=source_directory, max_workers=workers\n )\n\n for name, result in zip(filenames, results):\n # The results list is either `None` or an exception for each filename in\n # the input list, in order.\n\n if isinstance(result, Exception):\n print(\"Failed to upload {} due to exception: {}\".format(name, result))\n else:\n print(\"Uploaded {} to {}.\".format(name, bucket.name))\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)."]]