Search code examples
javascriptnode.jslinode

Linode Storage With NodeJs


I am new with linode. i see linode provide cloud storage just aws s3. i want to use it with my nodejs app.i can not find any sdk to do it like s3 any solution please help me .

any body tell me how can we upload file from nodejs to linode storage in javascript


Solution

  • new to linode too. Got my free $100 2 month trial and I figured I'd try the bucket feature.

    I used AWS S3 in the past, this is pretty much identical as far as the SDK goes. The only hurdle here was to configure the endpoint. With AWS S3 you put the region, with linode you put the endpoint instead. The list of endpoints is here:

    https://www.linode.com/docs/products/storage/object-storage/guides/urls/#cluster-url-s3-endpoint

    As you didn't mention if you wanted an example on the server (nodejs) or the browser, I'll go with the one I've got. It's for nodejs (server side).

    Steps

    I used node stable (currently 18.7). I set up package.json to start the index.js script (e.g. "scripts": {"start": "node index.js"}).

    Install aws-sdk

    npm i aws-sdk
    

    Code for index.js

    const S3 = require('aws-sdk/clients/s3')
    const fs = require('fs')
    
    const config = {
        endpoint: 'https://us-southeast-1.linodeobjects.com/',
        accessKeyId: 'BLEEPBLEEPBLEEP',
        secretAccessKey: 'BLOOPBLOOPBLOOP',
    }
    
    var s3 = new S3(config)
    
    function listObjects() {
        console.debug("List objects")
        const bucketParams = {
            Bucket: 'vol1'
        }
    
        s3.listObjects(bucketParams, (err, data) => {
            if(err) {
                console.error("Error ", err)
            } else {
                console.info("Objects vol1 ", data)
            }
        })
    }
    
    function uploadFile() {
        const fileStream = fs.createReadStream('./testfile.txt')
        var params = {Bucket: 'vol1', Key: 'testfile', Body: fileStream}
        s3.upload(params, function(err, data) {
            if(err) {
                console.error("Error uploading test file", err)
            } else {
                console.info("Test file uploaded ", data)
                listObjects()
            }
        })
    }
    
    // Start
    uploadFile()
    

    Run "npm start".

    Output I get:

    Test file uploaded  {
      ETag: '"0ea76c859582d95d2c2c0caf28e6d747"',
      Location: 'https://vol1.us-southeast-1.linodeobjects.com/testfile',
      key: 'testfile',
      Key: 'testfile',
      Bucket: 'vol1'
    }
    List objects
    Objects vol1  {
      IsTruncated: false,
      Marker: '',
      Contents: [
        {
          Key: 'Inflation isnt transitory.mp4',
          LastModified: 2023-01-10T15:38:42.045Z,
          ETag: '"4a77d408defc08c15fe42ad4e63fefbd"',
          ChecksumAlgorithm: [],
          Size: 58355708,
          StorageClass: 'STANDARD',
          Owner: [Object]
        },
        {
          Key: 'testfile',
          LastModified: 2023-02-13T20:28:01.178Z,
          ETag: '"0ea76c859582d95d2c2c0caf28e6d747"',
          ChecksumAlgorithm: [],
          Size: 18,
          StorageClass: 'STANDARD',
          Owner: [Object]
        }
      ],
      Name: 'vol1',
      Prefix: '',
      MaxKeys: 1000,
      CommonPrefixes: []
    }
    

    Adjust the config with your own creds/data center. Hope this helps.

    Note: if you want to upload files > 1gb, you'll want to use the multipart upload feature. It's a bit more complex, but this should get you started. Any AWS S3 code example should do, there are plenty out there.