s3api_handlers.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package s3api
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  7. "google.golang.org/grpc"
  8. "github.com/seaweedfs/seaweedfs/weed/pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. )
  11. var _ = filer_pb.FilerClient(&S3ApiServer{})
  12. func (s3a *S3ApiServer) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  13. return pb.WithGrpcClient(streamingMode, s3a.randomClientId, func(grpcConnection *grpc.ClientConn) error {
  14. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  15. return fn(client)
  16. }, s3a.option.Filer.ToGrpcAddress(), false, s3a.option.GrpcDialOption)
  17. }
  18. func (s3a *S3ApiServer) AdjustedUrl(location *filer_pb.Location) string {
  19. return location.Url
  20. }
  21. func (s3a *S3ApiServer) GetDataCenter() string {
  22. return s3a.option.DataCenter
  23. }
  24. func writeSuccessResponseXML(w http.ResponseWriter, r *http.Request, response interface{}) {
  25. s3err.WriteXMLResponse(w, r, http.StatusOK, response)
  26. s3err.PostLog(r, http.StatusOK, s3err.ErrNone)
  27. }
  28. func writeSuccessResponseEmpty(w http.ResponseWriter, r *http.Request) {
  29. s3err.WriteEmptyResponse(w, r, http.StatusOK)
  30. }
  31. func writeFailureResponse(w http.ResponseWriter, r *http.Request, errCode s3err.ErrorCode) {
  32. s3err.WriteErrorResponse(w, r, errCode)
  33. }
  34. func validateContentMd5(h http.Header) ([]byte, error) {
  35. md5B64, ok := h["Content-Md5"]
  36. if ok {
  37. if md5B64[0] == "" {
  38. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  39. }
  40. return base64.StdEncoding.DecodeString(md5B64[0])
  41. }
  42. return []byte{}, nil
  43. }