sign_url.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package sample
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "strings"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. )
  8. // SignURLSample signs URL sample
  9. func SignURLSample() {
  10. // Create bucket
  11. bucket, err := GetTestBucket(bucketName)
  12. if err != nil {
  13. HandleError(err)
  14. }
  15. // Put object
  16. signedURL, err := bucket.SignURL(objectKey, oss.HTTPPut, 60)
  17. if err != nil {
  18. HandleError(err)
  19. }
  20. var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
  21. err = bucket.PutObjectWithURL(signedURL, strings.NewReader(val))
  22. if err != nil {
  23. HandleError(err)
  24. }
  25. // Put object with option
  26. options := []oss.Option{
  27. oss.Meta("myprop", "mypropval"),
  28. oss.ContentType("image/tiff"),
  29. }
  30. signedURL, err = bucket.SignURL(objectKey, oss.HTTPPut, 60, options...)
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. err = bucket.PutObjectFromFileWithURL(signedURL, localFile, options...)
  35. if err != nil {
  36. HandleError(err)
  37. }
  38. // Get object
  39. signedURL, err = bucket.SignURL(objectKey, oss.HTTPGet, 60)
  40. if err != nil {
  41. HandleError(err)
  42. }
  43. body, err := bucket.GetObjectWithURL(signedURL)
  44. if err != nil {
  45. HandleError(err)
  46. }
  47. // Read content
  48. data, err := ioutil.ReadAll(body)
  49. body.Close()
  50. data = data // use data
  51. err = bucket.GetObjectToFileWithURL(signedURL, "mynewfile-1.jpg")
  52. if err != nil {
  53. HandleError(err)
  54. }
  55. // Delete the object and bucket
  56. err = DeleteTestBucketAndObject(bucketName)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. fmt.Println("SignURLSample completed")
  61. }