Skip to content

Commit

Permalink
Merge pull request #1 from yezzey-gp/patch-object-v2
Browse files Browse the repository at this point in the history
Added PatchObject method
  • Loading branch information
EinKrebs authored Dec 26, 2023
2 parents b40e44f + 612418f commit d242a65
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 0 deletions.
13 changes: 13 additions & 0 deletions example/service/s3/patchObject/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Example

This is an example using the AWS SDK for Go to patch object.


# Usage

The example uses the bucket name provided, one key for object, uploads `base_object.txt` file to S3, then edits
this file, replacing it's 14th to 23rd bytes with `"a"` characters.

```sh
go run -tags example patchObject.go <bucket> <key for object>
```
Binary file added example/service/s3/patchObject/base_object.txt
Binary file not shown.
1 change: 1 addition & 0 deletions example/service/s3/patchObject/insertion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaa
89 changes: 89 additions & 0 deletions example/service/s3/patchObject/patchObject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build example
// +build example

package main

import (
"github.com/yezzey-gp/aws-sdk-go/aws"
"github.com/yezzey-gp/aws-sdk-go/aws/endpoints"
"github.com/yezzey-gp/aws-sdk-go/aws/session"
"github.com/yezzey-gp/aws-sdk-go/service/s3"
"github.com/yezzey-gp/aws-sdk-go/service/s3/s3manager"

"fmt"
"log"
"os"
)

func main() {
if len(os.Args) < 3 {
log.Println("USAGE ERROR: go run -tags example putObjWithProcess.go <bucket> <key for object>")
return
}

const filename = "./base_object.txt"

bucket := os.Args[1]
key := os.Args[2]

myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == endpoints.S3ServiceID {
return endpoints.ResolvedEndpoint{
URL: "https://storage.yandexcloud.net",
SigningRegion: "ru-central1",
}, nil
}

return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}

sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("ru-central1"),
EndpointResolver: endpoints.ResolverFunc(myCustomResolver),
}))

file, err := os.Open(filename)
if err != nil {
log.Fatalf("failed to open file %v, %v", filename, err)
}

uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) {
u.PartSize = 5 * 1024 * 1024
u.LeavePartsOnError = true
})

output, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
})
if err != nil {
log.Fatalf("failed to put file %v, %v", filename, err)
return
}
fmt.Println()
log.Println(output.Location)

const patchFile = "./insertion.txt"

file, err = os.Open(patchFile)
if err != nil {
log.Fatalf("failed to open file %v, %v", patchFile, err)
}

input := &s3.PatchObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
ContentRange: aws.String("bytes 14-23/*"),
ContentLength: aws.Int64(10),
}

assTree := s3.New(sess)
patchOutput, err := assTree.PatchObject(input)
if err != nil {
log.Fatalf("could not patch: %s", err)
}

log.Printf("output: %#v\n", patchOutput)
}
87 changes: 87 additions & 0 deletions models/apis/s3/2006-03-01/api-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,19 @@
"requestChecksumRequired":true
}
},
"PatchObject":{
"name":"PatchObject",
"http":{
"method":"PATCH",
"requestUri":"/{Bucket}/{Key+}"
},
"input":{"shape":"PatchObjectRequest"},
"output":{"shape":"PatchObjectOutput"},
"httpChecksum":{
"requestAlgorithmMember":"ChecksumAlgorithm",
"requestChecksumRequired":false
}
},
"PutPublicAccessBlock":{
"name":"PutPublicAccessBlock",
"http":{
Expand Down Expand Up @@ -7734,6 +7747,80 @@
},
"payload":"Tagging"
},
"PatchAppendPartSize":{"type": "integer"},
"PatchedObjectInfo":{
"type":"structure",
"members":{
"ETag":{"shape":"ETag"},
"LastModified":{"shape":"LastModified"}
}
},
"PatchObjectOutput":{
"type":"structure",
"members":{
"Object":{"shape":"PatchedObjectInfo"}
}
},
"PatchObjectRequest":{
"type":"structure",
"required":[
"Bucket",
"Key",
"ContentRange"
],
"members":{
"Body":{
"shape":"Body",
"streaming":true
},
"Bucket":{
"shape":"BucketName",
"location":"uri",
"locationName":"Bucket"
},
"ContentLength":{
"shape":"ContentLength",
"location":"header",
"locationName":"Content-Length"
},
"ContentMD5":{
"shape":"ContentMD5",
"location":"header",
"locationName":"Content-MD5"
},
"ChecksumAlgorithm":{
"shape":"ChecksumAlgorithm",
"location":"header",
"locationName":"x-amz-sdk-checksum-algorithm"
},
"ContentRange":{
"shape":"ContentRange",
"location":"header",
"locationName":"Content-Range"
},
"IfMatch":{
"shape":"IfMatch",
"location":"header",
"locationName":"If-Match"
},
"IfUnmodifiedSince":{
"shape":"IfUnmodifiedSince",
"location":"header",
"locationName":"If-Unmodified-Since"
},
"Key":{
"shape":"ObjectKey",
"location":"uri",
"locationName":"Key"
},
"PatchAppendPartSize":{
"shape":"PatchAppendPartSize",
"location":"header",
"locationName":"X-Yc-S3-Patch-Append-Part-Size"
}
},
"payload":"Body"
},
"PutPublicAccessBlockRequest":{
"type":"structure",
"required":[
Expand Down
Loading

0 comments on commit d242a65

Please sign in to comment.