s3_storage_client.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package s3
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/credentials"
  8. "github.com/aws/aws-sdk-go/aws/request"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
  11. "github.com/aws/aws-sdk-go/service/s3"
  12. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  13. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  14. "github.com/seaweedfs/seaweedfs/weed/filer"
  15. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  16. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  17. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  18. "github.com/seaweedfs/seaweedfs/weed/util"
  19. )
  20. func init() {
  21. remote_storage.RemoteStorageClientMakers["s3"] = new(s3RemoteStorageMaker)
  22. }
  23. type s3RemoteStorageMaker struct{}
  24. func (s s3RemoteStorageMaker) HasBucket() bool {
  25. return true
  26. }
  27. func (s s3RemoteStorageMaker) Make(conf *remote_pb.RemoteConf) (remote_storage.RemoteStorageClient, error) {
  28. client := &s3RemoteStorageClient{
  29. supportTagging: true,
  30. conf: conf,
  31. }
  32. config := &aws.Config{
  33. Region: aws.String(conf.S3Region),
  34. Endpoint: aws.String(conf.S3Endpoint),
  35. S3ForcePathStyle: aws.Bool(conf.S3ForcePathStyle),
  36. S3DisableContentMD5Validation: aws.Bool(true),
  37. }
  38. if conf.S3AccessKey != "" && conf.S3SecretKey != "" {
  39. config.Credentials = credentials.NewStaticCredentials(conf.S3AccessKey, conf.S3SecretKey, "")
  40. }
  41. sess, err := session.NewSession(config)
  42. if err != nil {
  43. return nil, fmt.Errorf("create aws session: %v", err)
  44. }
  45. if conf.S3V4Signature {
  46. sess.Handlers.Sign.PushBackNamed(v4.SignRequestHandler)
  47. }
  48. sess.Handlers.Build.PushBack(func(r *request.Request) {
  49. r.HTTPRequest.Header.Set("User-Agent", "SeaweedFS/"+util.VERSION_NUMBER)
  50. })
  51. sess.Handlers.Build.PushFront(skipSha256PayloadSigning)
  52. client.conn = s3.New(sess)
  53. return client, nil
  54. }
  55. type s3RemoteStorageClient struct {
  56. conf *remote_pb.RemoteConf
  57. conn s3iface.S3API
  58. supportTagging bool
  59. }
  60. var _ = remote_storage.RemoteStorageClient(&s3RemoteStorageClient{supportTagging: true})
  61. func (s *s3RemoteStorageClient) Traverse(remote *remote_pb.RemoteStorageLocation, visitFn remote_storage.VisitFunc) (err error) {
  62. pathKey := remote.Path[1:]
  63. listInput := &s3.ListObjectsV2Input{
  64. Bucket: aws.String(remote.Bucket),
  65. ContinuationToken: nil,
  66. Delimiter: nil, // not aws.String("/"), iterate through all entries
  67. EncodingType: nil,
  68. ExpectedBucketOwner: nil,
  69. FetchOwner: nil,
  70. MaxKeys: nil, // aws.Int64(1000),
  71. Prefix: aws.String(pathKey),
  72. RequestPayer: nil,
  73. StartAfter: nil,
  74. }
  75. isLastPage := false
  76. for !isLastPage && err == nil {
  77. var localErr error
  78. listErr := s.conn.ListObjectsV2Pages(listInput, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
  79. for _, content := range page.Contents {
  80. key := *content.Key
  81. key = "/" + key
  82. dir, name := util.FullPath(key).DirAndName()
  83. if err := visitFn(dir, name, false, &filer_pb.RemoteEntry{
  84. RemoteMtime: (*content.LastModified).Unix(),
  85. RemoteSize: *content.Size,
  86. RemoteETag: *content.ETag,
  87. StorageName: s.conf.Name,
  88. }); err != nil {
  89. localErr = err
  90. return false
  91. }
  92. }
  93. listInput.ContinuationToken = page.NextContinuationToken
  94. isLastPage = lastPage
  95. return true
  96. })
  97. if listErr != nil {
  98. err = fmt.Errorf("list %v: %v", remote, listErr)
  99. }
  100. if localErr != nil {
  101. err = fmt.Errorf("process %v: %v", remote, localErr)
  102. }
  103. }
  104. return
  105. }
  106. func (s *s3RemoteStorageClient) ReadFile(loc *remote_pb.RemoteStorageLocation, offset int64, size int64) (data []byte, err error) {
  107. downloader := s3manager.NewDownloaderWithClient(s.conn, func(u *s3manager.Downloader) {
  108. u.PartSize = int64(4 * 1024 * 1024)
  109. u.Concurrency = 1
  110. })
  111. dataSlice := make([]byte, int(size))
  112. writerAt := aws.NewWriteAtBuffer(dataSlice)
  113. _, err = downloader.Download(writerAt, &s3.GetObjectInput{
  114. Bucket: aws.String(loc.Bucket),
  115. Key: aws.String(loc.Path[1:]),
  116. Range: aws.String(fmt.Sprintf("bytes=%d-%d", offset, offset+size-1)),
  117. })
  118. if err != nil {
  119. return nil, fmt.Errorf("failed to download file %s%s: %v", loc.Bucket, loc.Path, err)
  120. }
  121. return writerAt.Bytes(), nil
  122. }
  123. func (s *s3RemoteStorageClient) WriteDirectory(loc *remote_pb.RemoteStorageLocation, entry *filer_pb.Entry) (err error) {
  124. return nil
  125. }
  126. func (s *s3RemoteStorageClient) RemoveDirectory(loc *remote_pb.RemoteStorageLocation) (err error) {
  127. return nil
  128. }
  129. func (s *s3RemoteStorageClient) WriteFile(loc *remote_pb.RemoteStorageLocation, entry *filer_pb.Entry, reader io.Reader) (remoteEntry *filer_pb.RemoteEntry, err error) {
  130. fileSize := int64(filer.FileSize(entry))
  131. partSize := int64(8 * 1024 * 1024) // The minimum/default allowed part size is 5MB
  132. for partSize*1000 < fileSize {
  133. partSize *= 4
  134. }
  135. // Create an uploader with the session and custom options
  136. uploader := s3manager.NewUploaderWithClient(s.conn, func(u *s3manager.Uploader) {
  137. u.PartSize = partSize
  138. u.Concurrency = 1
  139. })
  140. // process tagging
  141. tags := ""
  142. var awsTags *string
  143. // openstack swift doesn't support s3 object tagging
  144. if s.conf.S3SupportTagging {
  145. for k, v := range entry.Extended {
  146. if len(tags) > 0 {
  147. tags = tags + "&"
  148. }
  149. tags = tags + k + "=" + string(v)
  150. }
  151. awsTags = aws.String(tags)
  152. }
  153. // Upload the file to S3.
  154. _, err = uploader.Upload(&s3manager.UploadInput{
  155. Bucket: aws.String(loc.Bucket),
  156. Key: aws.String(loc.Path[1:]),
  157. Body: reader,
  158. Tagging: awsTags,
  159. StorageClass: aws.String(s.conf.S3StorageClass),
  160. })
  161. //in case it fails to upload
  162. if err != nil {
  163. return nil, fmt.Errorf("upload to %s/%s%s: %v", loc.Name, loc.Bucket, loc.Path, err)
  164. }
  165. // read back the remote entry
  166. return s.readFileRemoteEntry(loc)
  167. }
  168. func toTagging(attributes map[string][]byte) *s3.Tagging {
  169. tagging := &s3.Tagging{}
  170. for k, v := range attributes {
  171. tagging.TagSet = append(tagging.TagSet, &s3.Tag{
  172. Key: aws.String(k),
  173. Value: aws.String(string(v)),
  174. })
  175. }
  176. return tagging
  177. }
  178. func (s *s3RemoteStorageClient) readFileRemoteEntry(loc *remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) {
  179. resp, err := s.conn.HeadObject(&s3.HeadObjectInput{
  180. Bucket: aws.String(loc.Bucket),
  181. Key: aws.String(loc.Path[1:]),
  182. })
  183. if err != nil {
  184. return nil, err
  185. }
  186. return &filer_pb.RemoteEntry{
  187. RemoteMtime: resp.LastModified.Unix(),
  188. RemoteSize: *resp.ContentLength,
  189. RemoteETag: *resp.ETag,
  190. StorageName: s.conf.Name,
  191. }, nil
  192. }
  193. func (s *s3RemoteStorageClient) UpdateFileMetadata(loc *remote_pb.RemoteStorageLocation, oldEntry *filer_pb.Entry, newEntry *filer_pb.Entry) (err error) {
  194. if reflect.DeepEqual(oldEntry.Extended, newEntry.Extended) {
  195. return nil
  196. }
  197. tagging := toTagging(newEntry.Extended)
  198. if len(tagging.TagSet) > 0 {
  199. _, err = s.conn.PutObjectTagging(&s3.PutObjectTaggingInput{
  200. Bucket: aws.String(loc.Bucket),
  201. Key: aws.String(loc.Path[1:]),
  202. Tagging: toTagging(newEntry.Extended),
  203. })
  204. } else {
  205. _, err = s.conn.DeleteObjectTagging(&s3.DeleteObjectTaggingInput{
  206. Bucket: aws.String(loc.Bucket),
  207. Key: aws.String(loc.Path[1:]),
  208. })
  209. }
  210. return
  211. }
  212. func (s *s3RemoteStorageClient) DeleteFile(loc *remote_pb.RemoteStorageLocation) (err error) {
  213. _, err = s.conn.DeleteObject(&s3.DeleteObjectInput{
  214. Bucket: aws.String(loc.Bucket),
  215. Key: aws.String(loc.Path[1:]),
  216. })
  217. return
  218. }
  219. func (s *s3RemoteStorageClient) ListBuckets() (buckets []*remote_storage.Bucket, err error) {
  220. resp, err := s.conn.ListBuckets(&s3.ListBucketsInput{})
  221. if err != nil {
  222. return nil, fmt.Errorf("list buckets: %v", err)
  223. }
  224. for _, b := range resp.Buckets {
  225. buckets = append(buckets, &remote_storage.Bucket{
  226. Name: *b.Name,
  227. CreatedAt: *b.CreationDate,
  228. })
  229. }
  230. return
  231. }
  232. func (s *s3RemoteStorageClient) CreateBucket(name string) (err error) {
  233. _, err = s.conn.CreateBucket(&s3.CreateBucketInput{
  234. ACL: nil,
  235. Bucket: aws.String(name),
  236. CreateBucketConfiguration: nil,
  237. GrantFullControl: nil,
  238. GrantRead: nil,
  239. GrantReadACP: nil,
  240. GrantWrite: nil,
  241. GrantWriteACP: nil,
  242. ObjectLockEnabledForBucket: nil,
  243. })
  244. if err != nil {
  245. return fmt.Errorf("%s create bucket %s: %v", s.conf.Name, name, err)
  246. }
  247. return
  248. }
  249. func (s *s3RemoteStorageClient) DeleteBucket(name string) (err error) {
  250. _, err = s.conn.DeleteBucket(&s3.DeleteBucketInput{
  251. Bucket: aws.String(name),
  252. })
  253. if err != nil {
  254. return fmt.Errorf("delete bucket %s: %v", name, err)
  255. }
  256. return
  257. }