s3_storage_client.go 8.2 KB

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