1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import { S3Client, PutObjectCommand, CopyObjectCommand, GetObjectCommand, DeleteObjectCommand, HeadObjectCommand, } from "@aws-sdk/client-s3"; import { fromIni } from "@aws-sdk/credential-provider-ini"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; import axios from "axios"; import fs from "fs";
enum OSSProviderType { S3, OSS, MINIO, } const getClient = (providerType = OSSProviderType.S3) => { if (providerType === OSSProviderType.OSS) { const aliConfig: { profiles: { access_key_id: string; access_key_secret: string }[]; } = JSON.parse( fs.readFileSync(`${process.env.HOME}/.aliyun/config.json`).toString() ); return new S3Client({ region: "oss-cn-hangzhou", credentials: { accessKeyId: aliConfig.profiles[0].access_key_id, secretAccessKey: aliConfig.profiles[0].access_key_secret, }, endpoint: "https://oss-cn-hangzhou.aliyuncs.com", apiVersion: "2006-03-01", }); } else if (providerType === OSSProviderType.MINIO) { const minioConfig: { aliases: { [k: string]: { url: string; accessKey: string; secretKey: string; api: string; path: string; }; }; } = JSON.parse( fs.readFileSync(`${process.env.HOME}/.mc/config.json`).toString() ); return new S3Client({ credentials: { accessKeyId: minioConfig.aliases.local.accessKey, secretAccessKey: minioConfig.aliases.local.secretKey, }, endpoint: minioConfig.aliases.local.url, forcePathStyle: true, }); } return new S3Client({ region: "cn-northwest-1", credentials: fromIni({ profile: "sandbox" }), }); };
const exec = async () => { const client = getClient(OSSProviderType.MINIO); const bucket = "nnsay-cn";
console.log(">>>>>>> putObject <<<<<<<"); const putResult = await client.send( new PutObjectCommand({ Bucket: bucket, Key: "putobject.json", Body: "hello", }) ); console.log("put object result: ", putResult.ETag); console.log(">>>>>>> copyObject <<<<<<<"); const copyResult = await client.send( new CopyObjectCommand({ Bucket: bucket, Key: "putobject-copy.json", CopySource: `${bucket}/putobject.json`, }) ); console.log("copy object result: ", copyResult.CopyObjectResult?.ETag);
console.log(">>>>>>> put signed url <<<<<<<"); const putSignedURL = await getSignedUrl( client, new PutObjectCommand({ Bucket: bucket, Key: "signedurlobject.json", }), { expiresIn: 600, } ); console.log("put signed url result: ", putSignedURL);
console.log(">>>>>>> upload by signedurl <<<<<<<"); await axios.put(putSignedURL, JSON.stringify({ hello: "Jimmy" }));
console.log(">>>>>>> get signed url <<<<<<<"); const getSignedURL = await getSignedUrl( client, new GetObjectCommand({ Bucket: bucket, Key: "signedurlobject.json", }), { expiresIn: 600, } ); console.log("get signed url result: ", getSignedURL);
console.log(">>>>>>> download by signedurl <<<<<<<"); const getSignedURLResult = await axios.get(getSignedURL); console.log("download by signedurl result: ", getSignedURLResult.data);
console.log(">>>>>>> getObject <<<<<<<"); const getResult = await client.send( new GetObjectCommand({ Bucket: bucket, Key: "signedurlobject.json", }) ); console.log( "get object(signedurl upload) result: %s", await getResult.Body?.transformToString("utf8") );
console.log(">>>>>>> deleteObject <<<<<<<"); const deleteResult = await client.send( new DeleteObjectCommand({ Bucket: bucket, Key: "putobject-copy.json", }) ); console.log( "delete object result: %s", deleteResult.$metadata.httpStatusCode );
const headResult = await client.send( new HeadObjectCommand({ Bucket: bucket, Key: "putobject.json", }) );
console.log("head object result: %s", headResult.ETag); };
exec() .then(() => { console.log("done"); }) .catch((err) => { console.log(err); });
|