s3_storage_client.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package s3
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/credentials"
  6. "github.com/aws/aws-sdk-go/aws/request"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
  9. "github.com/aws/aws-sdk-go/service/s3"
  10. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  11. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  12. "github.com/seaweedfs/seaweedfs/weed/filer"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  15. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. "io"
  18. "reflect"
  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 = 8
  139. })
  140. // process tagging
  141. tags := ""
  142. if s.supportTagging {
  143. for k, v := range entry.Extended {
  144. if len(tags) > 0 {
  145. tags = tags + "&"
  146. }
  147. tags = tags + k + "=" + string(v)
  148. }
  149. }
  150. // Upload the file to S3.
  151. _, err = uploader.Upload(&s3manager.UploadInput{
  152. Bucket: aws.String(loc.Bucket),
  153. Key: aws.String(loc.Path[1:]),
  154. Body: reader,
  155. Tagging: aws.String(tags),
  156. StorageClass: aws.String(s.conf.S3StorageClass),
  157. })
  158. //in case it fails to upload
  159. if err != nil {
  160. return nil, fmt.Errorf("upload to %s/%s%s: %v", loc.Name, loc.Bucket, loc.Path, err)
  161. }
  162. // read back the remote entry
  163. return s.readFileRemoteEntry(loc)
  164. }
  165. func toTagging(attributes map[string][]byte) *s3.Tagging {
  166. tagging := &s3.Tagging{}
  167. for k, v := range attributes {
  168. tagging.TagSet = append(tagging.TagSet, &s3.Tag{
  169. Key: aws.String(k),
  170. Value: aws.String(string(v)),
  171. })
  172. }
  173. return tagging
  174. }
  175. func (s *s3RemoteStorageClient) readFileRemoteEntry(loc *remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) {
  176. resp, err := s.conn.HeadObject(&s3.HeadObjectInput{
  177. Bucket: aws.String(loc.Bucket),
  178. Key: aws.String(loc.Path[1:]),
  179. })
  180. if err != nil {
  181. return nil, err
  182. }
  183. return &filer_pb.RemoteEntry{
  184. RemoteMtime: resp.LastModified.Unix(),
  185. RemoteSize: *resp.ContentLength,
  186. RemoteETag: *resp.ETag,
  187. StorageName: s.conf.Name,
  188. }, nil
  189. }
  190. func (s *s3RemoteStorageClient) UpdateFileMetadata(loc *remote_pb.RemoteStorageLocation, oldEntry *filer_pb.Entry, newEntry *filer_pb.Entry) (err error) {
  191. if reflect.DeepEqual(oldEntry.Extended, newEntry.Extended) {
  192. return nil
  193. }
  194. tagging := toTagging(newEntry.Extended)
  195. if len(tagging.TagSet) > 0 {
  196. _, err = s.conn.PutObjectTagging(&s3.PutObjectTaggingInput{
  197. Bucket: aws.String(loc.Bucket),
  198. Key: aws.String(loc.Path[1:]),
  199. Tagging: toTagging(newEntry.Extended),
  200. })
  201. } else {
  202. _, err = s.conn.DeleteObjectTagging(&s3.DeleteObjectTaggingInput{
  203. Bucket: aws.String(loc.Bucket),
  204. Key: aws.String(loc.Path[1:]),
  205. })
  206. }
  207. return
  208. }
  209. func (s *s3RemoteStorageClient) DeleteFile(loc *remote_pb.RemoteStorageLocation) (err error) {
  210. _, err = s.conn.DeleteObject(&s3.DeleteObjectInput{
  211. Bucket: aws.String(loc.Bucket),
  212. Key: aws.String(loc.Path[1:]),
  213. })
  214. return
  215. }
  216. func (s *s3RemoteStorageClient) ListBuckets() (buckets []*remote_storage.Bucket, err error) {
  217. resp, err := s.conn.ListBuckets(&s3.ListBucketsInput{})
  218. if err != nil {
  219. return nil, fmt.Errorf("list buckets: %v", err)
  220. }
  221. for _, b := range resp.Buckets {
  222. buckets = append(buckets, &remote_storage.Bucket{
  223. Name: *b.Name,
  224. CreatedAt: *b.CreationDate,
  225. })
  226. }
  227. return
  228. }
  229. func (s *s3RemoteStorageClient) CreateBucket(name string) (err error) {
  230. _, err = s.conn.CreateBucket(&s3.CreateBucketInput{
  231. ACL: nil,
  232. Bucket: aws.String(name),
  233. CreateBucketConfiguration: nil,
  234. GrantFullControl: nil,
  235. GrantRead: nil,
  236. GrantReadACP: nil,
  237. GrantWrite: nil,
  238. GrantWriteACP: nil,
  239. ObjectLockEnabledForBucket: nil,
  240. })
  241. if err != nil {
  242. return fmt.Errorf("%s create bucket %s: %v", s.conf.Name, name, err)
  243. }
  244. return
  245. }
  246. func (s *s3RemoteStorageClient) DeleteBucket(name string) (err error) {
  247. _, err = s.conn.DeleteBucket(&s3.DeleteBucketInput{
  248. Bucket: aws.String(name),
  249. })
  250. if err != nil {
  251. return fmt.Errorf("delete bucket %s: %v", name, err)
  252. }
  253. return
  254. }