当前位置: 首页 > article >正文

AWS S3 JavaScript SDK(v3)常用操作

安装 aws s3 sdk

npm install @aws-sdk/client-s3

配置

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

[default]
aws_access_key_id=<...>
aws_secret_access_key=<...>
region=<...>

S3 SDK常用桶操作

获取桶列表

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);
    }
}

创建桶

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);
    }
}

删除桶

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常用对象操作

获取对象列表

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);
    }
}

获取对象

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);
    }
}

上传对象

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);
    }
}

删除对象

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);
    }
}

http://www.kler.cn/a/385096.html

相关文章:

  • 大数据分库分表方案
  • CSS文本样式与浮动
  • Golang--协程和管道
  • YOLOv6-4.0部分代码阅读笔记-effidehead_lite.py
  • 【p2p、分布式,区块链笔记 Torrent】WebTorrent 的lt_donthave插件
  • Sigrity Power SI 3D-EM Inductance Extraction模式如何进行电感的提取操作指导(一)
  • ReactPress 是什么?
  • 独立站怎么搭建,英文网站制作
  • Moment.js、Day.js、Miment,日期时间库怎么选?
  • 安全合规:沃尔玛自养号测评技术搭建要点
  • apk因检测是否使用代理无法抓包绕过方式
  • CUDA解说
  • net core Autofac 替换默认的服务容器 DI,微软自动的容器 不支持命名选项的
  • Spring框架---JDBC模板技术
  • 新华三H3CNE网络工程师认证—VLAN的配置
  • 23种设计模式总结
  • PyQt6应用程序中,如何实现多种语言支持
  • ajax关于axios库的运用小案例
  • 网页版五子棋——匹配模块(客户端开发)
  • 一、文心一言问答系统为什么要分对话,是否回学习上下文?二、文心一言是知识检索还是大模型检索?三、文心一言的词向量、词语种类及多头数量
  • Python中处理Excel的基本概念(如工作簿、工作表等)
  • 【数据结构】一文讲解线性表之顺序表概念及其基本操作(附C语言源码)
  • 鸿蒙的进击之路
  • 享元模式在 JDK 中的应用解析
  • MySQL 多数据库备份与恢复,包括查询,函数,SP
  • 笔记--(5)、acl ACL