AWS S3 JavaScript SDK(v3)常用操作

安装 aws s3 sdk

shell 复制代码
npm install @aws-sdk/client-s3

配置

创建 ~/.aws/credentials 文件,添加以下配置项:

shell 复制代码
[default]
aws_access_key_id=<...>
aws_secret_access_key=<...>
region=<...>

S3 SDK常用桶操作

获取桶列表

typescript 复制代码
import {
    S3Client,
    Bucket,
    paginateListBuckets,
} from "@aws-sdk/client-s3";

async function listBuckets() {
    const client = new S3Client({});

    try {
        const buckets: Bucket[] = [];
        for await (const page of paginateListBuckets({ client }, {})) {
            if (page.Buckets) {
                buckets.push(...page.Buckets);
            }
        }
        console.log("Buckets: ", buckets);
    } catch (error) {
        console.error(error);
    }
}

创建桶

typescript 复制代码
import {
    S3Client,
    Bucket,
    CreateBucketCommand,
    waitUntilBucketExists,
} from "@aws-sdk/client-s3";

async function createBucket(bucket: string) {
    const client = new S3Client({});

    try {
        const { Location } = await client.send(
            new CreateBucketCommand({
                Bucket: bucket,
            })
        );
        await waitUntilBucketExists({ client, maxWaitTime: 1000 }, { Bucket: bucket });
        console.log(`Bucket created with location ${Location}`);
    } catch (error) {
        console.error(error);
    }
}

删除桶

typescript 复制代码
import {
    S3Client,
    Bucket,
    DeleteBucketCommand,
    waitUntilBucketExists,
} from "@aws-sdk/client-s3";

async function deleteBucket(bucket: string) {
    const client = new S3Client({});

    try {
        await client.send(
            new DeleteBucketCommand({
                Bucket: bucket,
            })
        );
        console.log("Bucket was deleted.");
    } catch (error) {
        console.error(error);
    }
}

S3 SDK常用对象操作

获取对象列表

typescript 复制代码
import {
    S3Client,
    Bucket,
    paginateListObjectsV2,
} from "@aws-sdk/client-s3";

async function listObjects(bucket: string) {
    const client = new S3Client({});

    try {
        const objects = [];
        const paginator = paginateListObjectsV2({ client, pageSize: 2 }, { Bucket: bucket });
        for await (const page of paginator) {
            if (page.Contents) {
                objects.push(page.Contents.map((o) => o.Key));
            }
        }
        console.log("Objects: ", objects);
    } catch (error) {
        console.error(error);
    }
}

获取对象

typescript 复制代码
import {
    S3Client,
    Bucket,
    GetObjectCommand,
} from "@aws-sdk/client-s3";

async function getObject(bucket: string, key: string) {
    const client = new S3Client({});

    try {
        const response = await client.send(
            new GetObjectCommand({
                Bucket: bucket,
                Key: key,
            })
        );
        if (response.Body) {
            console.log(await response.Body.transformToString());
        }
    } catch (error) {
        console.error(error);
    }
}

上传对象

typescript 复制代码
import {
    S3Client,
    Bucket,
    PutObjectCommand,
} from "@aws-sdk/client-s3";
import { readFile } from "node:fs/promises";

async function putObject(bucket: string, key: string, filePath: string) {
    const client = new S3Client({});

    try {
        const response = await client.send(
            new PutObjectCommand({
                Bucket: bucket,
                Key: key,
                Body: await readFile(filePath),
            })
        );
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}

删除对象

typescript 复制代码
import {
    S3Client,
    Bucket,
    DeleteObjectCommand,
} from "@aws-sdk/client-s3";

async function deleteObject(bucket: string, key: string) {
    const client = new S3Client({});

    try {
        const response = await client.send(
            new DeleteObjectCommand({
                Bucket: bucket,
                Key: key,
            })
        );
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}
相关推荐
kongxx3 小时前
AWS S3在客户端应用不能使用aws-sdk场景下的文件上传与下载
aws
Elastic 中国社区官方博客3 小时前
如何将数据从 AWS S3 导入到 Elastic Cloud - 第 3 部分:Elastic S3 连接器
大数据·elasticsearch·搜索引擎·云计算·全文检索·可用性测试·aws
九河云3 小时前
如何选择适合的AWS EC2实例类型
服务器·云计算·aws
九河云18 小时前
AWS账号注册费用详解:新用户是否需要付费?
服务器·云计算·aws
神一样的老师18 小时前
利用亚马逊AWS IoT核心和MQTT进行数据采集的综合指南
云计算·aws
danns8881 天前
什么是 AWS PrivateLink
云计算·aws
shiran小坚果1 天前
AWS RDS MySQL内存使用
数据库·mysql·云计算·database·aws
sealaugh323 天前
aws(学习笔记第九课) 使用AWS的网络存储EBS
笔记·学习·aws