presigned_put.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/aws/aws-sdk-go/service/s3"
  9. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. // Downloads an item from an S3 Bucket in the region configured in the shared config
  15. // or AWS_REGION environment variable.
  16. //
  17. // Usage:
  18. //
  19. // go run presigned_put.go
  20. //
  21. // For this exampl to work, the domainName is needd
  22. //
  23. // weed s3 -domainName=localhost
  24. func main() {
  25. util_http.InitGlobalHttpClient()
  26. h := md5.New()
  27. content := strings.NewReader(stringContent)
  28. content.WriteTo(h)
  29. // Initialize a session in us-west-2 that the SDK will use to load
  30. // credentials from the shared credentials file ~/.aws/credentials.
  31. sess, err := session.NewSession(&aws.Config{
  32. Region: aws.String("us-east-1"),
  33. Endpoint: aws.String("http://localhost:8333"),
  34. })
  35. // Create S3 service client
  36. svc := s3.New(sess)
  37. putRequest, output := svc.PutObjectRequest(&s3.PutObjectInput{
  38. Bucket: aws.String("dev"),
  39. Key: aws.String("testKey"),
  40. })
  41. fmt.Printf("output: %+v\n", output)
  42. md5s := base64.StdEncoding.EncodeToString(h.Sum(nil))
  43. putRequest.HTTPRequest.Header.Set("Content-MD5", md5s)
  44. url, err := putRequest.Presign(15 * time.Minute)
  45. if err != nil {
  46. fmt.Println("error presigning request", err)
  47. return
  48. }
  49. fmt.Println(url)
  50. req, err := http.NewRequest("PUT", url, strings.NewReader(stringContent))
  51. req.Header.Set("Content-MD5", md5s)
  52. if err != nil {
  53. fmt.Println("error creating request", url)
  54. return
  55. }
  56. resp, err := http.DefaultClient.Do(req)
  57. if err != nil {
  58. fmt.Printf("error put request: %v\n", err)
  59. return
  60. }
  61. defer util_http.CloseResponse(resp)
  62. fmt.Printf("response: %+v\n", resp)
  63. }
  64. var stringContent = `Generate a Pre-Signed URL for an Amazon S3 PUT Operation with a Specific Payload
  65. You can generate a pre-signed URL for a PUT operation that checks whether users upload the correct content. When the SDK pre-signs a request, it computes the checksum of the request body and generates an MD5 checksum that is included in the pre-signed URL. Users must upload the same content that produces the same MD5 checksum generated by the SDK; otherwise, the operation fails. This is not the Content-MD5, but the signature. To enforce Content-MD5, simply add the header to the request.
  66. The following example adds a Body field to generate a pre-signed PUT operation that requires a specific payload to be uploaded by users.
  67. `