|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { S3 } from "@aws-sdk/client-s3"; |
| 5 | +import { afterAll, beforeAll, describe, expect, test as it } from "vitest"; |
| 6 | + |
| 7 | +import { getIntegTestResources } from "../../../../tests/e2e/get-integ-test-resources"; |
| 8 | + |
| 9 | +describe("@aws-sdk/client-s3", () => { |
| 10 | + let client: S3; |
| 11 | + let Bucket: string; |
| 12 | + let region: string; |
| 13 | + |
| 14 | + beforeAll(async () => { |
| 15 | + const integTestResourcesEnv = await getIntegTestResources(); |
| 16 | + Object.assign(process.env, integTestResourcesEnv); |
| 17 | + |
| 18 | + region = process?.env?.AWS_SMOKE_TEST_REGION as string; |
| 19 | + Bucket = process?.env?.AWS_SMOKE_TEST_BUCKET as string; |
| 20 | + |
| 21 | + client = new S3({ region }); |
| 22 | + }); |
| 23 | + |
| 24 | + async function putObject(Body: string, Key: string) { |
| 25 | + await client.putObject({ |
| 26 | + Bucket, |
| 27 | + Key, |
| 28 | + Body, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + async function putBuffer(Body: Uint8Array, Key: string) { |
| 33 | + await client.putObject({ |
| 34 | + Bucket, |
| 35 | + Key, |
| 36 | + Body, |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + async function copyObject(from: string, to: string) { |
| 41 | + await client.copyObject({ |
| 42 | + Bucket, |
| 43 | + Key: to, |
| 44 | + CopySource: `/${Bucket}/${from}`, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + async function getObject(Key: string) { |
| 49 | + return ( |
| 50 | + await client.getObject({ |
| 51 | + Bucket, |
| 52 | + Key, |
| 53 | + }) |
| 54 | + ).Body?.transformToString(); |
| 55 | + } |
| 56 | + |
| 57 | + async function objectExists(Key: string) { |
| 58 | + try { |
| 59 | + await client.headObject({ |
| 60 | + Bucket, |
| 61 | + Key, |
| 62 | + }); |
| 63 | + return true; |
| 64 | + } catch (e) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + async function deleteObject(Key: string) { |
| 70 | + await client.deleteObject({ |
| 71 | + Bucket, |
| 72 | + Key, |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + describe("CRUD operations", () => { |
| 77 | + it("should perform basic CRUD operations on objects", async () => { |
| 78 | + await putObject("world", "hello"); |
| 79 | + expect(await objectExists("hello")).toBe(true); |
| 80 | + |
| 81 | + const obj = await getObject("hello"); |
| 82 | + expect(obj).toBe("world"); |
| 83 | + |
| 84 | + await putObject("new world", "hello"); |
| 85 | + expect(await objectExists("hello")).toBe(true); |
| 86 | + |
| 87 | + const updatedObj = await getObject("hello"); |
| 88 | + expect(updatedObj).toBe("new world"); |
| 89 | + |
| 90 | + await deleteObject("hello"); |
| 91 | + expect(await objectExists("hello")).toBe(false); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("Content length", () => { |
| 96 | + it("should handle content length", async () => { |
| 97 | + await client.putObject({ |
| 98 | + Bucket, |
| 99 | + Key: "contentlength", |
| 100 | + Body: "foo", |
| 101 | + ContentLength: 3, |
| 102 | + }); |
| 103 | + expect(await objectExists("contentlength")).toBe(true); |
| 104 | + |
| 105 | + const obj = await getObject("contentlength"); |
| 106 | + expect(obj).toBe("foo"); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("Multi-byte strings", () => { |
| 111 | + it("should handle multi-byte strings", async () => { |
| 112 | + await putObject("åß∂ƒ©", "multi"); |
| 113 | + expect(await objectExists("multi")).toBe(true); |
| 114 | + |
| 115 | + const obj = await client.getObject({ |
| 116 | + Bucket, |
| 117 | + Key: "multi", |
| 118 | + }); |
| 119 | + |
| 120 | + const str = String(await obj.Body?.transformToString()); |
| 121 | + |
| 122 | + expect(str).toBe("åß∂ƒ©"); |
| 123 | + expect(str.length).toBe(5); |
| 124 | + expect(obj.ContentLength).toBe(11); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe("Object copying", () => { |
| 129 | + it("should copy objects", async () => { |
| 130 | + await putObject("world", "hello"); |
| 131 | + await copyObject("hello", "byebye"); |
| 132 | + |
| 133 | + expect(await objectExists("byebye")).toBe(true); |
| 134 | + |
| 135 | + const obj = await getObject("byebye"); |
| 136 | + expect(obj).toBe("world"); |
| 137 | + |
| 138 | + await deleteObject("byebye"); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe("Empty objects", () => { |
| 143 | + it("should handle empty strings", async () => { |
| 144 | + await putObject("", "blank"); |
| 145 | + expect(await objectExists("blank")).toBe(true); |
| 146 | + |
| 147 | + const obj = await getObject("blank"); |
| 148 | + expect(obj).toBe(""); |
| 149 | + expect(obj).toHaveLength(0); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe("Buffers", () => { |
| 154 | + it("should handle empty buffers", async () => { |
| 155 | + await putBuffer(Buffer.alloc(0), "emptybuffer"); |
| 156 | + expect(await objectExists("emptybuffer")).toBe(true); |
| 157 | + |
| 158 | + const obj = await getObject("emptybuffer"); |
| 159 | + expect(obj).toHaveLength(0); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should handle small buffers", async () => { |
| 163 | + await putBuffer(Buffer.alloc(1048576), "smallbuffer"); |
| 164 | + expect(await objectExists("smallbuffer")).toBe(true); |
| 165 | + |
| 166 | + const obj = await getObject("smallbuffer"); |
| 167 | + expect(obj).toHaveLength(1048576); |
| 168 | + }); |
| 169 | + |
| 170 | + it("should handle large buffers", async () => { |
| 171 | + await putBuffer(Buffer.alloc(20971520), "largebuffer"); |
| 172 | + expect(await objectExists("largebuffer")).toBe(true); |
| 173 | + |
| 174 | + const obj = await getObject("largebuffer"); |
| 175 | + expect(obj).toHaveLength(20971520); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe("Files", () => { |
| 180 | + beforeAll(async () => { |
| 181 | + fs.writeFileSync(path.join(__dirname, "emptyfile"), "a".repeat(0)); |
| 182 | + fs.writeFileSync(path.join(__dirname, "smallfile"), "a".repeat(1048576)); |
| 183 | + fs.writeFileSync(path.join(__dirname, "largefile"), "a".repeat(20971520)); |
| 184 | + }); |
| 185 | + |
| 186 | + afterAll(async () => { |
| 187 | + fs.rmSync(path.join(__dirname, "emptyfile")); |
| 188 | + fs.rmSync(path.join(__dirname, "smallfile")); |
| 189 | + fs.rmSync(path.join(__dirname, "largefile")); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should handle empty files", async () => { |
| 193 | + await client.putObject({ |
| 194 | + Bucket, |
| 195 | + Key: "emptyfile", |
| 196 | + Body: fs.createReadStream(path.join(__dirname, "emptyfile")), |
| 197 | + }); |
| 198 | + expect(await objectExists("emptyfile")).toBe(true); |
| 199 | + |
| 200 | + const obj = await getObject("emptyfile"); |
| 201 | + expect(obj).toHaveLength(0); |
| 202 | + }); |
| 203 | + |
| 204 | + it("should handle small files", async () => { |
| 205 | + await client.putObject({ |
| 206 | + Bucket, |
| 207 | + Key: "smallfile", |
| 208 | + Body: fs.createReadStream(path.join(__dirname, "smallfile")), |
| 209 | + }); |
| 210 | + expect(await objectExists("smallfile")).toBe(true); |
| 211 | + |
| 212 | + const obj = await getObject("smallfile"); |
| 213 | + expect(obj).toHaveLength(1048576); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should handle large files", async () => { |
| 217 | + await client.putObject({ |
| 218 | + Bucket, |
| 219 | + Key: "largefile", |
| 220 | + Body: fs.createReadStream(path.join(__dirname, "largefile")), |
| 221 | + }); |
| 222 | + expect(await objectExists("largefile")).toBe(true); |
| 223 | + |
| 224 | + const obj = await getObject("largefile"); |
| 225 | + expect(obj).toHaveLength(20971520); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + // describe("Checksums", () => { |
| 230 | + // it("should verify data integrity", async () => { |
| 231 | + // const data = "SOME SAMPLE DATA"; |
| 232 | + // const checksum = generateMD5(data); |
| 233 | + // |
| 234 | + // await putObject(data, "checksummed_data"); |
| 235 | + // expect(await objectExists("checksummed_data")).toBe(true); |
| 236 | + // |
| 237 | + // const obj = await getObject("checksummed_data"); |
| 238 | + // expect(obj).toBe(data); |
| 239 | + // expect(obj).toHaveLength(16); |
| 240 | + // expect(generateMD5(obj)).toBe(checksum); |
| 241 | + // }); |
| 242 | + // }); |
| 243 | + |
| 244 | + // describe("Pre-signed URLs", () => { |
| 245 | + // it("should handle pre-signed PUT/GET", async () => { |
| 246 | + // const putUrl = await getPresignedUrl("PUT", "presigned", ""); |
| 247 | + // await putWithPresignedUrl(putUrl, "PRESIGNED BODY CONTENTS"); |
| 248 | + // |
| 249 | + // const getUrl = await getPresignedUrl("GET", "presigned"); |
| 250 | + // const response = await getWithPresignedUrl(getUrl); |
| 251 | + // expect(response).toBe("PRESIGNED BODY CONTENTS"); |
| 252 | + // }); |
| 253 | + // |
| 254 | + // it("should validate checksums", async () => { |
| 255 | + // const putUrl = await getPresignedUrl("PUT", "hello", "CHECKSUMMED"); |
| 256 | + // const response = await putWithPresignedUrl(putUrl, "NOT CHECKSUMMED"); |
| 257 | + // expect(response).toContain("SignatureDoesNotMatch"); |
| 258 | + // }); |
| 259 | + // }); |
| 260 | + // |
| 261 | + // describe("Proxy support", () => { |
| 262 | + // it("should work through proxy", async () => { |
| 263 | + // await putObject("world", "proxy_object"); |
| 264 | + // expect(await objectExists("proxy_object")).toBe(true); |
| 265 | + // |
| 266 | + // const obj = await getObject("proxy_object"); |
| 267 | + // expect(obj).toBe("world"); |
| 268 | + // |
| 269 | + // await deleteObject("proxy_object"); |
| 270 | + // expect(await objectExists("proxy_object")).toBe(false); |
| 271 | + // |
| 272 | + // await teardownProxy(); |
| 273 | + // }); |
| 274 | + // }); |
| 275 | +}, 60_000); |
0 commit comments