This topic describes how to manage Object Storage Service (OSS) in the Node.js environment, such as view buckets and upload objects.

Note To facilitate modification, a file named app.js is created in this topic. The following code for each operation is described in sync mode. For more information about how to create clients in the sample code, see Initialization.

Query the list of buckets

Add the following content to the end of the app.js file and call listBuckets to query buckets:

async function listBuckets () {
  try {
    let result = await client.listBuckets();
    console.log(result)
}    
    catch(err) {
    console.log(err)
  }
}

listBuckets();            

You can run the node app.js script and view the response.

For more information about bucket-related operations, visit GitHub.

Query objects in a bucket

Modify the app.js file, and call list to query the objects in a bucket:

client.useBucket('examplebucket');
async function list () {
  try {
    let result = await client.list({
      'max-keys': 5
    })
    console.log(result)
  } catch (err) {
    console.log (err)
  }
}
list();

You can run the node app.js script and view the response.

Upload an object

Modify the app.js file and call put to upload a single object:

client.useBucket('examplebucket');

async function put () {
  try {
    let result = await client.put('exampleobject.txt', 'D:\\localpath\\examplefile.txt');
    console.log(result);
   } catch (err) {
     console.log (err);
   }
}

put();           

Download an object

Modify the app.js file, and call get to download a single object:

client.useBucket('examplebucket');

async function get () {
  try {
    let result = await client.get('exampleobject.txt');
    console.log(result);
  } catch (err) {
    console.log (err);
  }
}

get();            

Delete an object

Modify the app.js file and call delete to delete an object:

client.useBucket('examplebucket');

async function delete () {
  try {
    let result = await client.delete('exampleobject.txt');
    console.log(result);
  } catch (err) {
    console.log (err);
  }
}

delete();            

For more information about object-related operations, visit GitHub.