forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from yezzey-gp/patch-object-v2
Added PatchObject method
- Loading branch information
Showing
7 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aaaaaaaaaa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.