@@ -20,6 +20,7 @@ requestChecksumCalculation: "WHEN_REQUIRED",
2020responseChecksumValidation : " WHEN_REQUIRED" ,
2121```
2222
23+ If checksum is needed, R2 supports ` SHA-1 ` and ` SHA-256 ` algorithms.
2324:::
2425
2526``` ts
@@ -106,6 +107,95 @@ console.log(
106107// }
107108```
108109
110+ ### With SHA-1/SHA-256 checksum algorithms
111+
112+ You can also use SHA-1 and SHA-256 algorithms for checksum.
113+
114+ ``` ts
115+ import {
116+ S3Client ,
117+ ListBucketsCommand ,
118+ ListObjectsV2Command ,
119+ GetObjectCommand ,
120+ PutObjectCommand ,
121+ } from " @aws-sdk/client-s3" ;
122+ import { createHash } from " node:crypto" ;
123+
124+ const S3 = new S3Client ({
125+ region: " auto" ,
126+ endpoint: ` https://${ACCOUNT_ID }.r2.cloudflarestorage.com ` ,
127+ credentials: {
128+ accessKeyId: ACCESS_KEY_ID ,
129+ secretAccessKey: SECRET_ACCESS_KEY ,
130+ },
131+ });
132+
133+ // Create an array buffer
134+ const arrayBuffer = await OBJECT .arrayBuffer ();
135+
136+ // Create SHA-1 hash of the object to upload
137+ const ChecksumSHA1 = createHash (" sha256" )
138+ .update (Buffer .from (arrayBuffer ))
139+ .digest (" base64" );
140+
141+ // Upload the object with the checksums
142+ console .log (
143+ await S3 .send (
144+ new PutObjectCommand ({
145+ Bucket: BUCKET_NAME ,
146+ Key: OBJECT_KEY ,
147+ Body: OBJECT ,
148+ ChecksumSHA1 ,
149+ }),
150+ ),
151+ );
152+
153+ // {
154+ // '$metadata': {
155+ // httpStatusCode: 200,
156+ // requestId: undefined,
157+ // extendedRequestId: undefined,
158+ // cfId: undefined,
159+ // attempts: 1,
160+ // totalRetryDelay: 0
161+ // },
162+ // ETag: '"355801ab7ccb9ffddcb4c47e8cd61584"',
163+ // ChecksumSHA1: '6MMnUIGMVR/u6AO3uCoUcSRnmzQ=',
164+ // VersionId: '7e6b8ae6e2198a8c1acf76598af339ef'
165+ // }
166+
167+ // Create SHA-256 hash of the object to upload
168+ const ChecksumSHA256 = createHash (" sha256" )
169+ .update (Buffer .from (arrayBuffer ))
170+ .digest (" base64" );
171+
172+ // Upload the object with the checksums
173+ console .log (
174+ await S3 .send (
175+ new PutObjectCommand ({
176+ Bucket: BUCKET_NAME ,
177+ Key: OBJECT_KEY ,
178+ Body: OBJECT ,
179+ ChecksumSHA256 ,
180+ }),
181+ ),
182+ );
183+
184+ // {
185+ // '$metadata': {
186+ // httpStatusCode: 200,
187+ // requestId: undefined,
188+ // extendedRequestId: undefined,
189+ // cfId: undefined,
190+ // attempts: 1,
191+ // totalRetryDelay: 0
192+ // },
193+ // ETag: '"f0d8680d5c596202dd81afa17428c65f"',
194+ // ChecksumSHA256: 'jSIKqrDnDlJg3pSnXflg9QJyzGiexsvIa3tCCRfb3DA=',
195+ // VersionId: '7e6b8ae42793fb4a693f020ff58ef8d0'
196+ // }
197+ ```
198+
109199## Generate presigned URLs
110200
111201You can also generate presigned links that can be used to share public read or write access to a bucket temporarily.
0 commit comments