Skip to content

Commit 142edfe

Browse files
committed
add example for Go and Python
1 parent 75ee232 commit 142edfe

File tree

2 files changed

+157
-7
lines changed

2 files changed

+157
-7
lines changed

src/content/docs/r2/examples/aws/aws-sdk-go.mdx

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: aws-sdk-go
33
pcx_content_type: example
4-
54
---
65

7-
import { Render } from "~/components"
6+
import { Render } from "~/components";
87

9-
<Render file="keys" /><br/>
8+
<Render file="keys" />
9+
<br />
1010

1111
This example uses version 2 of the [aws-sdk-go](https://github.com/aws/aws-sdk-go-v2) package. You must pass in the R2 configuration credentials when instantiating your `S3` service client:
1212

@@ -16,10 +16,11 @@ Client version `1.73.0` introduced a modification to the default checksum behavi
1616
To mitigate, users can use `1.72.3` or add the following to their config:
1717

1818
```go
19-
config.WithRequestChecksumCalculation(0)
20-
config.WithResponseChecksumValidation(0)
19+
config.WithRequestChecksumCalculation(2)
20+
config.WithResponseChecksumValidation(2)
2121
```
2222

23+
If checksum is needed, R2 supports `SHA-1` and `SHA-256` algorithms.
2324
:::
2425

2526
```go
@@ -93,6 +94,106 @@ func main() {
9394
}
9495
```
9596

97+
### With SHA-1/SHA-256 checksum algorithms
98+
99+
You can also use SHA-1 and SHA-256 algorithms for checksum.
100+
101+
```go
102+
package main
103+
104+
import (
105+
"bytes"
106+
"context"
107+
"crypto/sha1"
108+
"crypto/sha256"
109+
"encoding/base64"
110+
"fmt"
111+
"log"
112+
"os"
113+
"github.com/aws/aws-sdk-go-v2/aws"
114+
"github.com/aws/aws-sdk-go-v2/config"
115+
"github.com/aws/aws-sdk-go-v2/credentials"
116+
"github.com/aws/aws-sdk-go-v2/service/s3"
117+
)
118+
119+
func calculateSHA256(data []byte) string {
120+
hash := sha256.New()
121+
hash.Write(data)
122+
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
123+
}
124+
125+
func calculateSHA1(data []byte) string {
126+
hash := sha1.New()
127+
hash.Write(data)
128+
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
129+
}
130+
131+
func main() {
132+
var bucketName = "sdk-example"
133+
var accountId = "<accountid>"
134+
var accessKeyId = "<access_key_id>"
135+
var accessKeySecret = "<access_key_secret>"
136+
137+
cfg, err := config.LoadDefaultConfig(context.TODO(),
138+
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")),
139+
config.WithRegion("auto"),
140+
)
141+
if err != nil {
142+
log.Fatal(err)
143+
}
144+
145+
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
146+
o.BaseEndpoint = aws.String(fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId))
147+
})
148+
149+
// Read your object data (example with a file)
150+
data, err := os.ReadFile("/path/to/file")
151+
if err != nil {
152+
log.Fatal(err)
153+
}
154+
155+
// Calcualte SHA1
156+
checksumSHA1 := calculateSHA1(data)
157+
158+
// Create the upload input
159+
uploadInputForSHA1 := &s3.PutObjectInput{
160+
Bucket: aws.String(bucketName),
161+
Key: aws.String(objectKey),
162+
Body: bytes.NewReader(data),
163+
ChecksumSHA1: aws.String(checksumSHA1),
164+
}
165+
166+
// Upload the object with SHA1 checksum
167+
resultSHA1, errSHA1 := client.PutObject(context.TODO(), uploadInputForSHA1)
168+
if errSHA1 != nil {
169+
log.Fatal(errSHA1)
170+
}
171+
172+
fmt.Printf("Upload successful with SHA1 checksum: %+v\n", resultSHA1)
173+
174+
// Calculate SHA256
175+
checksumSHA256 := calculateSHA256(data)
176+
177+
// Create the upload input
178+
uploadInputForSHA256 := &s3.PutObjectInput{
179+
Bucket: aws.String(bucketName),
180+
Key: aws.String(objectKey),
181+
Body: bytes.NewReader(data),
182+
ChecksumSHA256: aws.String(checksumSHA256),
183+
}
184+
185+
// Upload the object with SHA256 checksum
186+
resultSHA256, errSHA256 := client.PutObject(context.TODO(), uploadInputForSHA256)
187+
if errSHA256 != nil {
188+
log.Fatal(errSHA256)
189+
}
190+
191+
fmt.Printf("Upload successful with SHA256 checksum: %+v\n", resultSHA256)
192+
193+
}
194+
195+
```
196+
96197
## Generate presigned URLs
97198

98199
You can also generate presigned links that can be used to temporarily share public write access to a bucket.

src/content/docs/r2/examples/aws/boto3.mdx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ Client version `1.36.0` introduced a modification to the default checksum behavi
1616
To mitigate, users can use `1.35.99` or add the following to their s3 resource config:
1717

1818
```python
19-
request_checksum_calculation = 'WHEN_REQUIRED',
20-
response_checksum_validation = 'WHEN_REQUIRED'
19+
request_checksum_calculation = 'when_required',
20+
response_checksum_validation = 'when_required'
2121
```
2222

23+
If checksum is needed, R2 supports `SHA-1` and `SHA-256` algorithms.
2324
:::
2425

2526
```python
@@ -69,3 +70,51 @@ Objects:
6970
- cat.png
7071
- todos.txt
7172
```
73+
74+
### With SHA-1/SHA-256 checksum algorithms
75+
76+
You can also use SHA-1 and SHA-256 algorithms for checksum.
77+
78+
```python
79+
import boto3
80+
import hashlib
81+
import base64
82+
83+
def calculate_sha1_base64(data):
84+
"""Calculate SHA-1 hash and return base64 encoded string."""
85+
sha1_hash = hashlib.sha1(data).digest()
86+
return base64.b64encode(sha1_hash).decode('utf-8')
87+
88+
def calculate_sha256_base64(data):
89+
"""Calculate SHA-256 hash and return base64 encoded string."""
90+
sha256_hash = hashlib.sha256(data).digest()
91+
return base64.b64encode(sha256_hash).decode('utf-8')
92+
93+
s3 = boto3.client(
94+
service_name ="s3",
95+
endpoint_url = 'https://<accountid>.r2.cloudflarestorage.com',
96+
aws_access_key_id = '<access_key_id>',
97+
aws_secret_access_key = '<access_key_secret>',
98+
region_name="<location>", # Must be one of: wnam, enam, weur, eeur, apac, auto
99+
)
100+
101+
# Calculate SHA1
102+
sha1 = calculate_sha1_base64(file_data)
103+
104+
# Uplodad file to R2 with SHA1 checksum
105+
response = s3.put_object(
106+
Body=file_data,
107+
Bucket=bucket_name,
108+
Key='sha1.txt',
109+
ChecksumSHA1=sha1)
110+
111+
# Calculate SHA256
112+
sha256 = calculate_sha256_base64(file_data)
113+
114+
# Uplodad file to R2 with SHA256 checksum
115+
response = s3.put_object(
116+
Body=file_data,
117+
Bucket=bucket_name,
118+
Key='sha256.txt',
119+
ChecksumSHA256=sha256)
120+
```

0 commit comments

Comments
 (0)