•  Home
  •  Dashboard
  •  Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
  •  Hosting Services
    • Infrastructure
      • iColocation
    • Compute
      • cMetal
      • cVirtual
    • Storage
      • sObject
      • sBlock
    • Networking
      • nIP Transit
      • nWavelength
    • Protection
      • pBackup
      • pDDoS
  •  Solutions
    • Ecommerce
    • Finance
    • Gaming
    • Hosting
    • Management
    • Security
    • System Integrator
  •  Support
    • Community
    • Knowledge Base
    • Open A Ticket
    • Status
  •  USA & Canada: 800-933-1517
  •  International: 626-549-2801
  •  Email: sales@psychz.net
  • Services
    • new-colocation-header-img
      Infrastructure
      • iColocation
    • new-compute-header-img
      Compute
      • cMetal
      • cVirtual
    • new-storage-header-img
      Storage
      • sObject
      • sBlock
    • new-networking-header-img
      Networking
      • nIP Transit
      • nWavelength
    • new-protection-header-img
      Protection
      • pBackup
      • pDDoS
  • Solutions
    • Ecommerce
    • Security
    • Gaming
    • Hosting
    • Management
    • Finance
    • System Integrator
  • Dashboard

sObject - Javascript Examples

  • Home
  • Client
  • Knowledgebase
  • Storage
  • sObject
  • sObject - Javascript Examples

Table Of Content

    Related Articles

    • sObject – Bucket Policy Examples
    • sObject – Sub-User Permissions
    • s3Express Guide
    • sObject - PHP Examples
    • sObject - Python Examples
    • sObject - Configuring CORS and Bucket Policies Using S3 Browser
    • sObject - SSE (Server Side Encryption) Guide
    • sObject - CNAME Guide
    • sObject - CrossFTP Guide
    • sObject - WinSCP Guide
    • sObject - S3 Browser Guide
    • sObject - Keys
    • sObject - Buckets
    • sObject - Access Using S3tools
    • sObject - Limits and Best Practices
    • sObject - Delete
    • sObject - Intro
    • sObject - Create

    sObject - Javascript Examples

    Publisher: Psychz Networks March 21,2025
    • Prerequisites
    • Installing PHP SDK
    • Creating Connection
    • Listing Buckets
    • Creating Buckets
    • Listing Bucket's Content
    • Deleting Bucket
    • Creating Object
    • Changing Object's ACL
    • Deleting Object
    • Downloading Object (To a File)
    • Generating Object Download URL
    • Conclusion

    Integrating sObject with JavaScript

    This guide demonstrates how to interact with sObject storage using JavaScript. The following examples use the AWS SDK for JavaScript. Replace the placeholder values with your sObject endpoint, access key, and secret key.

    Installing the JavaScript SDK

    To get started, ensure you have Node.js and npm installed. Install the AWS SDK for JavaScript by running:

    npm install aws-sdk

    Creating a Connection

    Create a connection to sObject by configuring the S3 client with sObject endpoint and credentials. Save the following code in a JavaScript file (for example, psychz_sobject_connection.js):

    const AWS = require('aws-sdk');
    
    const s3 = new AWS.S3({
      endpoint: 'http://your-object-storage-endpoint',
      accessKeyId: 'your-access-key',
      secretAccessKey: 'your-secret-key',
      s3ForcePathStyle: true, // Required for S3-compatible services
      signatureVersion: 'v4'
    });
    
    console.log("Connected to object storage successfully.");

    Listing Owned Buckets

    Use the listBuckets method to retrieve and list all buckets owned by your account:

    s3.listBuckets((err, data) => {
      if (err) {
        console.error('Error listing buckets:', err);
      } else {
        console.log('Buckets:');
        data.Buckets.forEach(bucket => {
          console.log(bucket.Name);
        });
      }
    });

    Creating A Bucket

    Create a new bucket by specifying a unique bucket name:

    const bucketName = 'my-new-bucket';
    
    s3.createBucket({ Bucket: bucketName }, (err, data) => {
      if (err) {
        console.error('Error creating bucket:', err);
      } else {
        console.log(`Bucket "${bucketName}" created successfully.`);
      }
    });

    Listing A Bucket's Content

    List all objects stored in a bucket:

    s3.listObjects({ Bucket: bucketName }, (err, data) => {
      if (err) {
        console.error('Error listing bucket contents:', err);
      } else {
        console.log(`Contents of "${bucketName}":`);
        data.Contents.forEach(object => {
          console.log(object.Key);
        });
      }
    });

    Deleting A Bucket

    Ensure the bucket is empty before deletion. This example deletes all objects in the bucket and then the bucket itself.

    // List objects in the bucket
    s3.listObjects({ Bucket: bucketName }, (err, data) => {
      if (err) {
        console.error('Error listing bucket objects:', err);
      } else {
        const objects = data.Contents.map(object => ({ Key: object.Key }));
        if (objects.length > 0) {
          s3.deleteObjects({
            Bucket: bucketName,
            Delete: { Objects: objects }
          }, (err, data) => {
            if (err) {
              console.error('Error deleting objects:', err);
            } else {
              // Delete the bucket
              s3.deleteBucket({ Bucket: bucketName }, (err, data) => {
                if (err) {
                  console.error('Error deleting bucket:', err);
                } else {
                  console.log(`Bucket "${bucketName}" deleted successfully.`);
                }
              });
            }
          });
        } else {
          // If bucket is already empty
          s3.deleteBucket({ Bucket: bucketName }, (err, data) => {
            if (err) {
              console.error('Error deleting bucket:', err);
            } else {
              console.log(`Bucket "${bucketName}" deleted successfully.`);
            }
          });
        }
      }
    });

    Creating An Object

    Upload content to a new object in your bucket using the putObject method:

    const objectKey = 'sample.txt';
    const content = 'This is a sample object content.';
    
    s3.putObject({
      Bucket: bucketName,
      Key: objectKey,
      Body: content
    }, (err, data) => {
      if (err) {
        console.error('Error creating object:', err);
      } else {
        console.log(`Object "${objectKey}" created successfully in bucket "${bucketName}".`);
      }
    });

    Uploading An Object

    To upload a file, use a file stream. Ensure you have the fs module available.

    const fs = require('fs');
    const filePath = 'path/to/your/file.txt';
    const uploadKey = 'uploaded-file.txt';
    const fileStream = fs.createReadStream(filePath);
    
    fileStream.on('error', function(err) {
      console.error('File Error:', err);
    });
    
    s3.upload({
      Bucket: bucketName,
      Key: uploadKey,
      Body: fileStream
    }, (err, data) => {
      if (err) {
        console.error('Error uploading file:', err);
      } else {
        console.log(`File uploaded successfully. Location: ${data.Location}`);
      }
    });

    Download An Object

    const downloadKey = 'uploaded-file.txt';
    
    s3.getObject({ Bucket: bucketName, Key: downloadKey }, (err, data) => {
      if (err) {
        console.error('Error downloading object:', err);
      } else {
        // The downloaded content is available in data.Body as a Buffer
        console.log(`Downloaded object content: ${data.Body.toString()}`);
      }
    });

    Delete An Object

    s3.deleteObject({ Bucket: bucketName, Key: objectKey }, (err, data) => {
      if (err) {
        console.error('Error deleting object:', err);
      } else {
        console.log(`Object "${objectKey}" deleted successfully from bucket "${bucketName}".`);
      }
    });

    Change An Object's ACL

    s3.putObjectAcl({
      Bucket: bucketName,
      Key: objectKey,
      ACL: 'public-read'
    }, (err, data) => {
      if (err) {
        console.error('Error changing object ACL:', err);
      } else {
        console.log(`ACL for object "${objectKey}" changed successfully.`);
      }
    });

    Generate Object Download URL (Signed/Unsigned)

    Signed URL (Temporary Access)

    Generate a signed URL that expires after a specified time, or construct an unsigned URL for publicly accessible objects.

    // Signed URL (expires in 20 minutes)
    const signedUrl = s3.getSignedUrl('getObject', {
      Bucket: bucketName,
      Key: objectKey,
      Expires: 60 * 20 // 20 minutes
    });
    console.log('Signed URL:', signedUrl);

    Unsigned URL (For Public Objects)

    If the object is public, you can manually construct the URL:

    // Unsigned URL (for public objects)
    const unsignedUrl = `${s3.endpoint.href.replace(/\/$/, '')}/${bucketName}/${objectKey}`;
    console.log('Unsigned URL:', unsignedUrl);

    Conclusion

    These examples provide a starting point for integrating sObject into JavaScript applications. Adjust the code as needed to match your environment and security requirements. This guide provided sample code for installing the SDK, establishing a connection, and performing various bucket and object operations.

    Happy coding!

    Views: (1670) Votes: (0)

    Related Articles

    • sObject – Bucket Policy Examples
    • sObject – Sub-User Permissions
    • s3Express Guide
    • sObject - PHP Examples
    • sObject - Python Examples
    • sObject - Configuring CORS and Bucket Policies Using S3 Browser
    • sObject - SSE (Server Side Encryption) Guide
    • sObject - CNAME Guide
    • sObject - CrossFTP Guide
    • sObject - WinSCP Guide
    • sObject - S3 Browser Guide
    • sObject - Keys
    • sObject - Buckets
    • sObject - Access Using S3tools
    • sObject - Limits and Best Practices
    • sObject - Delete
    • sObject - Intro
    • sObject - Create
    Copyright © 2026 Psychz Networks,
    A Profuse Solutions Inc Company
    Hosting Services
    • Catalog
    Infrastructure
    • iColocation
    Compute
    • cMetal
    • cVirtual
    Storage
    • sObject
    • sBlock
    Networking
    • nIP Transit
    • nWavelength
    Protection
    • pBackup
    • pDDoS
    Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
    Policies
    • Acceptable Usage Policy
    • Privacy Policy
    • Service Level Agreement
    • Terms and Conditions
    Support
    • Community
    • Knowledge Base
    • Open A Ticket
    • Status
    Get In Touch
    • Psychz Networks,
      A Profuse Solutions Company
      611 Wilshire Blvd #300
      Los Angeles,California 90017
      USA
    • US/Canada: 800-933-1517
    • International: 626-549-2801