NCS - Documents
Go to Portal
  • NIPA Cloud Space Overview
    • Welcome to NIPA Cloud Space documentation
    • NCS User Account
      • Create NIPA Cloud Space Account
      • Loging in to Nipa Cloud Space
      • Reset NCS Password
      • Activating Two-Factor Authentication
      • Deactivating Two-Factor Authentication
    • Co-working Projects
      • Create Co-working Project
      • Manage Project Member
      • Exporting Resource List
    • Billing & Wallet
      • Topup a Project Wallet
      • Redeem Voucher
      • Insufficient Wallet Balance
  • COMPUTE
    • Compute Instance
      • What is OS Status?
      • Launching Compute Instance
      • Managing Instance
        • Stop
        • Start
        • Restart
        • Resize (Change Machine Type)
        • Take Snapshot
        • Auto Backup
        • Reset Linux "root" Password
        • Reset Windows "Administrator" Password
      • SSH to Linux Compute Instance
        • Converting Key Pair for PuTTy
        • Windows Client using Key Pair
        • Windows Client using Password
        • MacOS/Linux using Key Pairs
        • MacOS/Linux using Password
      • Remote Desktop to Windows Instance
      • Setting Docker Image Caching
      • Renaming Instance
      • Exporting Instance List
      • How to change Compute Instance hostname
      • How to install QEMU Guest Agent
      • How to fix update kernel for RHEL
      • How to Update Rocky Linux 9
      • How to Upgrade Rocky Linux to 9.4
      • How to change RDP port on Windows
    • Compute Image
      • Create Image From a Bootable Volume
      • Importing Your Own Image
      • Export Image
      • Share Image to Between Projects
      • Exporting Image List
    • Key Pair
      • Managing Key Pair(s)
      • Creating a New Key Pair
      • Importing an Existing Key Pair
    • Deployment Script
      • Managing Deployment Script
      • Create a Deployment Script
      • Clone a Deployment Script
      • Edit a Deployment Script
      • Delete a Deployment Script
  • STORAGE
    • Block Storage
      • Managing Volume(s)
        • Create From Blank
        • Create From an Image
        • Create From a Volume
        • Create From a Snapshot
        • Transferring a Volume
        • Accepting a Tranferred Volume
        • Renaming a Volume
        • How to Change Volume Type
      • Managing Snapshot(s)
        • Create a Snapshot
        • Renaming a Volume Snapshot
      • Exporting Volume and Volume Snapshot List
    • Object Storage (S3)
      • Migrate file S3 AWS to S3 NIPA
      • Create an Object Storage Bucket
      • Delete an Object Storage Bucket
      • Create an Object Storage Sub-User
      • Regenerate Sub-User's Access Key
      • Revoke Sub-User's Access Key
      • Create Bucket Policy
      • Bucket Versioning
      • Access S3 Bucket with Cyberduck
        • Upload Files to a Bucket
        • Share File via Public Link
      • Access S3 Bucket with s3cmd
        • Basic command
        • การ set ACL สำหรับการเปิดใช้งาน Objects แบบ public
        • การสร้าง Presigned URL สำหรับการใช้งานชั่วคราว
      • Mount S3 Bucket on instances with s3fs-fuse
      • Mount the S3 bucket on the Windows
      • Access S3 buckets With AWS S3 Client SDK
        • S3Client Configuration
        • Basic Command
        • Multipart Upload
      • Access S3 buckets with internal network for NCS instance
      • Delete Lifecycle Policies
      • Move Objects Lifecycle Script
      • Configure a static website using S3 Bucket
    • NIPA Drive
      • Purchasing a Drive
  • NETWORKING
    • Networking
      • Managing VPC Network(s)
        • Create a Network
        • DHCP Setting
        • Create Port
        • Create Router
      • Managing Security Group(s)
        • Create a New Security Group
        • Create Security Group Presets
      • Managing External IP(s)
        • Create an External IP
        • Exporting External IP List
      • NAT Gateway with Ubuntu (VM) แบบ Host Route
      • NAT and VPN Gateway on NCS with Pfsense-2.6.0
  • LOAD BALANCING
    • Load Balancer as a Service
      • Create Load Balancer
      • Using Network Load Balancing
      • Using Application Load Balancing
      • Renaming a Load Balancer
      • Exporting Load Balancer List
      • Monitoring Load Balancer Using Prometheus
    • SSL Certificate
      • Import SSL Certificate
  • DATABASE AS A SERVICE
    • SQL Database
      • Create SQL Database Instance
        • Create MySQL Database Instance
      • Manage SQL Database Instance
        • Reboot Database Service
        • Delete Database Instance
        • Online Extend Storage Size
        • Edit Allowed CIDR
      • Auto-Scaling SQL Database Storage
        • Enable Auto-Scaling
        • Disable Auto-Scaling
        • Edit Auto-Scaling
      • Manage SQL Database Root User
        • Enable Root User
        • Reset Root User Password
      • Manage SQL Database Schema
        • Create Database Schema
        • Delete Database Schema
      • Manage SQL Database User
        • Create Database User
        • Delete Database User
        • Reset Password
        • Edit Access
      • Manage SQL Database Backup
        • Create Backup
        • Create A New SQL Database Instance From Backup
        • Delete Backup
      • Manage SQL Database Logs
        • Enable Logs
        • Disable Logs
        • Refresh Logs
        • Load More Logs
      • Manage Monitoring User
        • Create Monitoring User
        • Delete Monitoring User
      • Monitor SQL Database with Percona Monitoring and Management (PMM)
  • SCHEDULING
    • Schedules
      • Create Schedule
    • Jobs
  • Public API
    • What is NCS Public API ?
      • Download NCS Project RC File
      • Getting Start with NCS Public API
        • Using OpenStack Client Tool
        • Using REST API
        • Terraform with Openstack
        • Auto-scaling OpenStack Instances with Senlin and Prometheus
          • Installation Prometheus
          • Installation Alertmanager
  • MIGRATION
    • Migrating Linux VM from vSphere to NCS
    • Migrating Windows VM from vSphere to NCS
  • Customer Support
    • Having Problem Before Access a Project
    • Having Problem In a Project
  • Tutorial
    • My First Website
    • Access MySQL Database With MySQL Workbench
    • Pritunl for VPN server
    • Install Rancher Server with Docker Quick Start
      • Create RKE2 Cluster via Rancher Dashboard
    • Install odoo18 with external database
    • How to use LBaaS for mysql Load Balancing
    • How to use Cloudflare with Nipa Cloud Space
  • Release Notes
    • v5.0.X (v5.0.0-now)
      • v5.0.0
      • v5.1.0
      • v5.2.0
      • v5.2.1
      • v5.2.2
      • v5.2.3
      • v5.2.4
      • v.5.3.0
      • v5.4.0
    • v4.19.X (v4.19.0-v4.19.3)
      • v4.19.0
      • v4.19.1
      • v4.19.2
      • v4.19.3
    • v4.18.X (v4.18.0-v4.18.2)
      • v4.18.0
      • v4.18.1
      • v4.18.2
    • v4.17.X (v4.17.0-v4.17.3)
      • v4.17.0
      • v4.17.0.1
      • v4.17.1
      • v4.17.2
      • v4.17.3
    • v4.16.X (v4.16.0-v4.16.5)
      • v4.16.0
      • v4.16.1
      • v4.16.2
      • v4.16.3
      • v4.16.4
      • v4.16.5
    • v4.15.X (v4.15.0-v4.15.9)
      • v4.15.0
      • v4.15.1
      • v4.15.2
      • v4.15.3
      • v4.15.4
      • v4.15.5
      • v4.15.6
      • v4.15.7
      • v4.15.8
      • v4.15.9
    • v.4.14.X (v4.14.0-v4.14.2)
      • v.4.14.0
      • v4.14.1
      • 4.14.2
Powered by GitBook
On this page
  • คำสั่งพื้นฐานสำหรับการใช้งานเบื้องต้น
  • Prerequisite
  • 1) ListBucketsCommand
  • สำหรับการเรียกดูรายชื่อ Buckets ภายใน project
  • 2) PutObjectsCommand
  • สำหรับการ upload ข้อมูลเข้าไปยัง Buckets
  • parameter ที่จำเป็นสำหรับการใช้งาน
  • 3) ListObjectsCommand
  • parameter ที่จำเป็นสำหรับการใช้งาน
  • 4) DeleteObjectCommand
  • parameter ที่จำเป็นสำหรับการใช้งาน
  • 5) CopyObjectCommand
  • parameter ที่จำเป็นสำหรับการใช้งาน
  • 6) GetObjectCommand
  • parameter ที่จำเป็นสำหรับการใช้งาน
  • 7) getSignedUrl
  • parameter ที่จำเป็นสำหรับการใช้งาน

Was this helpful?

  1. STORAGE
  2. Object Storage (S3)
  3. Access S3 buckets With AWS S3 Client SDK

Basic Command

คำสั่งพื้นฐานสำหรับการใช้งานเบื้องต้น

  • ListBucketsCommand

  • PutObjectsCommand

  • ListObjectsCommand

  • DeleteObjectCommand

  • CopyObjectCommand

  • GetObjectCommand

  • getSignedUrl

Prerequisite

  • ต้องมี bucket สร้างเอาไว้แล้ว (ขั้นตอนในการสร้าง bucket)

  • มีการสร้าง subuser และ ให้สิทการเข้าถึง bucket เอาไว้แล้ว (ขั้นตอนการสร้าง subuser และ access key)

1) ListBucketsCommand

สำหรับการเรียกดูรายชื่อ Buckets ภายใน project

การเรียกใช้ ListBucketsCommand ไม่จำเป็นต้องใส่ parameter ใดๆ

import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const input = { }
const command = new ListBucketsCommand(input);

try {
    const response = await client.send(command);
    console.log(response);
} catch (error) {
    console.log(error);
}

Result

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'tx0000000000000007f36ae-006542153a-b6569ee-NCP-BKK',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Buckets: [
    {
      Name: 's3-client-buckets',
      CreationDate: 2023-11-01T08:53:55.139Z
    }
  ],
  Owner: {
    DisplayName: 'DevOps-Prototype',
    ID: 'xxx'
  }
}

2) PutObjectsCommand

สำหรับการ upload ข้อมูลเข้าไปยัง Buckets

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket)

  • Body (ข้อมูลที่ต้องการอัพโหลด)

  • Key (ชื่อของไฟล์ที่อัพโหลดไปยัง Bucket)

ตัวอย่างการใช้งาน PutObjectsCommand โดยมี package fs สำหรับใช้ในการอ่านไฟล์

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import * as fs from 'fs';

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const ncs_bucket = 's3-client-buckets';
const source = 'path/to/file/file2upload.jpg';
const dest = 'uploadedfile.jpg';
const fileContent = fs.readFileSync(source);
const input = {
    Bucket: ncs_bucket,
    Body: fileContent,
    Key: dest
}
const command = new PutObjectCommand(input);

try {
    const response = await client.send(command);
    console.log(response);
} catch (error) {
    console.log(error);
}

Result

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'tx00000000000000035f84a-0065485672-b8c3f38-NCP-BKK',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ETag: '"16de86869221ac1c9c66b9397d9845a7"'
}

3) ListObjectsCommand

สำหรับการเรียกดูรายชื่อ Objects ภายใน Bucket

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket ที่ต้องการเรียกดู)

import { S3Client, ListObjectsCommand } from "@aws-sdk/client-s3";

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const ncs_bucket = 's3-client-buckets';
const input = {
    Bucket: ncs_bucket
}
const command = new ListObjectsCommand(input);

try {
    const response = await client.send(command);
    console.log(response);
} catch (error) {
    console.log(error);
}

Result

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'tx000000000000000371e77-0065485938-b8c3f38-NCP-BKK',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Contents: [
    {
      Key: 'uploadedfile.jpg',
      LastModified: 2023-11-06T03:10:44.618Z,
      ETag: '"16de86869221ac1c9c66b9397d9845a7"',
      Size: 7156660,
      StorageClass: 'STANDARD',
      Owner: [Object]
    }
  ],
  IsTruncated: false,
  Marker: '',
  MaxKeys: 1000,
  Name: 's3-client-buckets',
  Prefix: ''
}

4) DeleteObjectCommand

สำหรับการลบ Objects ภายใน Bucket

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket)

  • Key (ชื่อของไฟล์ที่ต้องการลบ)

import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const target = 'file2delete.dum'
const input = {
    Bucket: ncs_bucket,
    Key: target
}
const command = new DeleteObjectCommand(input);

try {
    const response = await client.send(command);
    console.log(response);
} catch (error) {
    console.log(error);
}

Result

{
  '$metadata': {
    httpStatusCode: 204,
    requestId: 'tx000000000000000378393-0065485a1e-b8c3f0e-NCP-BKK',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  }
}

5) CopyObjectCommand

สำหรับการ Copy Object ไปยัง Bucket อื่น หรือ path ใหม่บน Bucket

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket ปลายทาง)

  • CopySource (ข้อมูลที้ต้องการทำซ้ำ โดยมี format คือ /{source bucket name}/{source file})

  • Key (ไฟล์ปลายทางจากการทำซ้ำ)

** สำหรับ Credentials ที่ใช้งานนั้น จะต้องมีสิทธิ์ read จาก Bucket ต้นทาง และ สิทธิ์ write จาก Bucket ปลายทาง

import { S3Client, CopyObjectCommand } from "@aws-sdk/client-s3";

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials,
    forcePathStyle: false
});

const ncs_bucket = 's3-client-buckets';
const source = '/s3-client-buckets/source.file'
const target = 'target.file'
const input = {
    Bucket: ncs_bucket,
    CopySource: source,
    Key: target
}

const command = new CopyObjectCommand(input);

try {
    const response = await client.send(command);
    console.log(response);
} catch (error) {
    console.log(error);
}

Result

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'tx0000000000000003f747c-00654b118d-b947ae8-NCP-BKK',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  CopyObjectResult: {
    ETag: '16de86869221ac1c9c66b9397d9845a7',
    LastModified: 2023-11-08T04:41:49.846Z
  }
}

6) GetObjectCommand

สำหรับการเรียกดูข้อมูล หรือ การ Download Object บน Buckets

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket)

  • Key (ชื่อไฟล์ที่ต้องการ)

import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import * as fs from 'fs';

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const ncs_bucket = 's3-client-buckets';
const target_file = 'ubuntu.jpeg'
const input = { 
    Bucket: ncs_bucket,
    Key: target_file
}

const command = new GetObjectCommand(input);
try {
    const response = await client.send(command);
    const inputStream = response.Body;
    const downloadPath = 'ubuntu.jpeg';
    const outputStream = fs.createWriteStream(downloadPath);
    inputStream.pipe(outputStream);
    outputStream.on('finish', () => {
      console.log(`downloaded the file successfully`);
    });
} catch (error) {
    console.log(error);
}

7) getSignedUrl

การสร้าง url ให้ object สำหรับการเปิดใช้งานเฉพาะ object แบบ public ชั่วคราว

parameter ที่จำเป็นสำหรับการใช้งาน

  • Bucket (ชื่อของ Bucket)

  • Key (ไฟล์ข้อมูลที่ต้องการ)

  • expiresIn (ระยะเวลา(นาที)ที่ url จะสามารถใช้งานได้)

**ต้องทำการ install package ที่ใช้สำหรับการสร้าง presigned url เพิ่มเติม

npm install @aws-sdk/s3-request-presigner
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const client = new S3Client({
    region: ncs_region,
    endpoint: ncs_endpoint,
    credentials: ncs_credentials
});

const ncs_bucket = 's3-client-buckets';
const file2share = 'hello-world.jpeg'
const input = { 
    Bucket: ncs_bucket,
    Key: file2share
}

const command = new GetObjectCommand(input);

try {
    const response = await getSignedUrl(client, command, { expiresIn: 3600 });
		console.log(response);
} catch (error) {
    console.log(error);
}

ผลลัพธ์ที่ได้ จะเป็น url สำหรับการเปิดดูข้อมูล

https://s3-client-buckets.s3-bkk.nipa.cloud/hello-world.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=X2EGTHMCW02X0KVQS1B8%2F20231109%2FNCP-TH%2Fs3%2Faws4_request&X-Amz-Date=20231109T082113Z&X-Amz-Expires=3600&X-Amz-Signature=2763b0d070d2a4f3cd561ac9cfd4fb984c132e06c87a418fad3fb4a5014c288d&X-Amz-SignedHeaders=host&x-id=GetObject

อ้างอิง

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/

PreviousS3Client ConfigurationNextMultipart Upload

Last updated 10 months ago

Was this helpful?