Search code examples
google-cloud-storagedeno

Upload files to google storage with Deno


I am trying to upload files to Google Cloud Storage with Deno using the npm package "npm:/@google-cloud/storage"but I am getting an error due to a non-compatibility between Deno and Node modules: Error: Not implemented: crypto.Sign

Does anyone know an alternative method to upload files to Google Cloud Storage using Deno?


Solution

  • You can use Google Cloud REST API: https://cloud.google.com/storage/docs/uploading-objects?hl=en#rest-upload-objects

    const token = 'OAUTH TOKEN'
    const bucket = 'BUCKET_NAME'
    
    const file = await Deno.readFile('./sample.txt');
    
    const res = await fetch(`https://storage.googleapis.com/upload/storage/v1/b/${bucket}/o?uploadType=media&name=sample.txt`, {
        headers: {
            'Content-Type': 'text/plain',
            Authorization: `Bearer ${token}`
        },
        method: 'POST',
        body: file
    })
    
    const data = await res.json();
    

    To obtain a token you can use the OAuth 2.0 Playground and then refresh the token using the refresh token obtained there