Upload a file without authentication
Stay organized with collections
Save and categorize content based on your preferences.
Upload a file without authentication
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Upload a file without authentication\n\nCode sample\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 contents that you want to upload\n // const contents = 'these are my contents';\n\n // The new ID for your GCS file\n // const destFileName = 'your-new-file-name';\n\n // Imports the Google Cloud Node.js 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 uploadWithoutAuthentication() {\n const file = storage.bucket(bucketName).file(destFileName);\n\n // Returns an authenticated endpoint to which\n // you can make requests without credentials.\n const [location] = await file.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/file_2.html(); //auth required\n\n const options = {\n uri: location,\n resumable: true,\n validation: false,\n\n // Optional:\n // Set a generation-match precondition to avoid potential race conditions\n // and data corruptions. The request to upload 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 preconditionOpts: {ifGenerationMatch: generationMatchPrecondition},\n };\n\n // Passes the location to file.save so you don't need to\n // authenticate this call\n await file.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/file_2.html(contents, options);\n\n console.log(`${destFileName} uploaded to ${bucketName}`);\n }\n\n uploadWithoutAuthentication().catch(console.error);\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)."]]