diff --git a/app/di/wire.go b/app/di/wire.go index 5aa43c8..1780daf 100644 --- a/app/di/wire.go +++ b/app/di/wire.go @@ -22,10 +22,14 @@ type S3App struct { usecase.S3BucketLister // S3BucketDeleter is the usecase for deleting a S3 bucket. usecase.S3BucketDeleter - // S3BucketObjectsLister is the usecase for listing S3 bucket objects. - usecase.S3BucketObjectsLister - // S3BucketObjectsDeleter is the usecase for deleting S3 bucket objects. - usecase.S3BucketObjectsDeleter + // S3ObjectsLister is the usecase for listing S3 bucket objects. + usecase.S3ObjectsLister + // S3ObjectsDeleter is the usecase for deleting S3 bucket objects. + usecase.S3ObjectsDeleter + // S3ObjectUploader is the usecase for uploading a file to S3 bucket. + usecase.S3ObjectDownloader + // FileUploader is the usecase for uploading a file. + usecase.FileUploader } // NewS3App creates a new S3App. @@ -37,13 +41,17 @@ func NewS3App(ctx context.Context, profile model.AWSProfile, region model.Region external.S3BucketListerSet, external.S3BucketLocationGetterSet, external.S3BucketDeleterSet, - external.S3BucketObjectsListerSet, - external.S3BucketObjectsDeleterSet, + external.S3ObjectsListerSet, + external.S3ObjectsDeleterSet, + external.S3ObjectDownloaderSet, + external.S3ObjectUploaderSet, interactor.S3BucketCreatorSet, interactor.S3BucketListerSet, interactor.S3BucketDeleterSet, - interactor.S3BucketObjectsListerSet, - interactor.S3BucketObjectsDeleterSet, + interactor.S3ObjectsListerSet, + interactor.S3ObjectsDeleterSet, + interactor.S3ObjectDownloaderSet, + interactor.FileUploaderSet, newS3App, ) return nil, nil @@ -53,15 +61,19 @@ func newS3App( s3BucketCreator usecase.S3BucketCreator, s3BucketLister usecase.S3BucketLister, s3BucketDeleter usecase.S3BucketDeleter, - s3BucketObjectsLister usecase.S3BucketObjectsLister, - s3BucketObjectsDeleter usecase.S3BucketObjectsDeleter, + S3ObjectsLister usecase.S3ObjectsLister, + S3ObjectsDeleter usecase.S3ObjectsDeleter, + s3ObjectDownloader usecase.S3ObjectDownloader, + fileUploader usecase.FileUploader, ) *S3App { return &S3App{ - S3BucketCreator: s3BucketCreator, - S3BucketLister: s3BucketLister, - S3BucketDeleter: s3BucketDeleter, - S3BucketObjectsLister: s3BucketObjectsLister, - S3BucketObjectsDeleter: s3BucketObjectsDeleter, + S3BucketCreator: s3BucketCreator, + S3BucketLister: s3BucketLister, + S3BucketDeleter: s3BucketDeleter, + S3ObjectsLister: S3ObjectsLister, + S3ObjectsDeleter: S3ObjectsDeleter, + S3ObjectDownloader: s3ObjectDownloader, + FileUploader: fileUploader, } } @@ -88,7 +100,7 @@ func NewSpareApp(ctx context.Context, profile model.AWSProfile, region model.Reg external.OAICreatorSet, external.NewS3Client, external.S3BucketCreatorSet, - external.S3BucketObjectUploaderSet, + external.S3ObjectUploaderSet, external.S3BucketPublicAccessBlockerSet, external.S3BucketPolicySetterSet, interactor.CloudFrontCreatorSet, diff --git a/app/di/wire_gen.go b/app/di/wire_gen.go index c86bb32..02256ca 100644 --- a/app/di/wire_gen.go +++ b/app/di/wire_gen.go @@ -33,11 +33,18 @@ func NewS3App(ctx context.Context, profile model.AWSProfile, region model.Region interactorS3BucketLister := interactor.NewS3BucketLister(s3BucketLister, s3BucketLocationGetter) s3BucketDeleter := external.NewS3BucketDeleter(client) interactorS3BucketDeleter := interactor.NewS3BucketDeleter(s3BucketDeleter, s3BucketLocationGetter) - s3BucketObjectsLister := external.NewS3BucketObjectsLister(client) - interactorS3BucketObjectsLister := interactor.NewS3BucketObjectsLister(s3BucketObjectsLister) - s3BucketObjectsDeleter := external.NewS3BucketObjectsDeleter(client) - interactorS3BucketObjectsDeleter := interactor.NewS3BucketObjectsDeleter(s3BucketObjectsDeleter, s3BucketLocationGetter) - s3App := newS3App(interactorS3BucketCreator, interactorS3BucketLister, interactorS3BucketDeleter, interactorS3BucketObjectsLister, interactorS3BucketObjectsDeleter) + s3ObjectsLister := external.NewS3ObjectsLister(client) + interactorS3ObjectsLister := interactor.NewS3ObjectsLister(s3ObjectsLister) + s3ObjectsDeleter := external.NewS3ObjectsDeleter(client) + interactorS3ObjectsDeleter := interactor.NewS3ObjectsDeleter(s3ObjectsDeleter, s3BucketLocationGetter) + s3ObjectDownloader := external.NewS3ObjectDownloader(client) + interactorS3ObjectDownloader := interactor.NewS3ObjectDownloader(s3ObjectDownloader) + s3ObjectUploader := external.NewS3ObjectUploader(client) + fileUploaderOptions := &interactor.FileUploaderOptions{ + S3ObjectUploader: s3ObjectUploader, + } + fileUploader := interactor.NewFileUploader(fileUploaderOptions) + s3App := newS3App(interactorS3BucketCreator, interactorS3BucketLister, interactorS3BucketDeleter, interactorS3ObjectsLister, interactorS3ObjectsDeleter, interactorS3ObjectDownloader, fileUploader) return s3App, nil } @@ -62,9 +69,9 @@ func NewSpareApp(ctx context.Context, profile model.AWSProfile, region model.Reg if err != nil { return nil, err } - s3BucketObjectUploader := external.NewS3BucketObjectUploader(s3Client) + s3ObjectUploader := external.NewS3ObjectUploader(s3Client) fileUploaderOptions := &interactor.FileUploaderOptions{ - S3BucketObjectUploader: s3BucketObjectUploader, + S3ObjectUploader: s3ObjectUploader, } fileUploader := interactor.NewFileUploader(fileUploaderOptions) s3BucketCreator := external.NewS3BucketCreator(s3Client) @@ -90,11 +97,17 @@ type S3App struct { // S3BucketLister is the usecase for listing S3 buckets. // S3BucketDeleter is the usecase for deleting a S3 bucket. - usecase.S3BucketObjectsLister - // S3BucketObjectsLister is the usecase for listing S3 bucket objects. - usecase.S3BucketObjectsDeleter + usecase.S3ObjectsLister + usecase. + // S3ObjectsLister is the usecase for listing S3 bucket objects. + S3ObjectsDeleter + usecase.S3ObjectDownloader - // S3BucketObjectsDeleter is the usecase for deleting S3 bucket objects. + // S3ObjectsDeleter is the usecase for deleting S3 bucket objects. + + // S3ObjectUploader is the usecase for uploading a file to S3 bucket. + usecase.FileUploader + // FileUploader is the usecase for uploading a file. } @@ -102,15 +115,19 @@ func newS3App( s3BucketCreator usecase.S3BucketCreator, s3BucketLister usecase.S3BucketLister, s3BucketDeleter usecase.S3BucketDeleter, - s3BucketObjectsLister usecase.S3BucketObjectsLister, - s3BucketObjectsDeleter usecase.S3BucketObjectsDeleter, + S3ObjectsLister usecase.S3ObjectsLister, + S3ObjectsDeleter usecase.S3ObjectsDeleter, + s3ObjectDownloader usecase.S3ObjectDownloader, + fileUploader usecase.FileUploader, ) *S3App { return &S3App{ - S3BucketCreator: s3BucketCreator, - S3BucketLister: s3BucketLister, - S3BucketDeleter: s3BucketDeleter, - S3BucketObjectsLister: s3BucketObjectsLister, - S3BucketObjectsDeleter: s3BucketObjectsDeleter, + S3BucketCreator: s3BucketCreator, + S3BucketLister: s3BucketLister, + S3BucketDeleter: s3BucketDeleter, + S3ObjectsLister: S3ObjectsLister, + S3ObjectsDeleter: S3ObjectsDeleter, + S3ObjectDownloader: s3ObjectDownloader, + FileUploader: fileUploader, } } diff --git a/app/domain/service/s3.go b/app/domain/service/s3.go index 8a06c54..46e25d6 100644 --- a/app/domain/service/s3.go +++ b/app/domain/service/s3.go @@ -69,8 +69,8 @@ type S3BucketDeleter interface { DeleteS3Bucket(ctx context.Context, input *S3BucketDeleterInput) (*S3BucketDeleterOutput, error) } -// S3BucketObjectsDeleterInput is the input of the DeleteBucketObjects method. -type S3BucketObjectsDeleterInput struct { +// S3ObjectsDeleterInput is the input of the DeleteBucketObjects method. +type S3ObjectsDeleterInput struct { // Bucket is the name of the bucket to delete. Bucket model.Bucket // Region is the region of the bucket that you want to delete. @@ -79,52 +79,60 @@ type S3BucketObjectsDeleterInput struct { S3ObjectSets model.S3ObjectIdentifierSets } -// S3BucketObjectsDeleterOutput is the output of the DeleteBucketObjects method. -type S3BucketObjectsDeleterOutput struct{} +// S3ObjectsDeleterOutput is the output of the DeleteBucketObjects method. +type S3ObjectsDeleterOutput struct{} -// S3BucketObjectsDeleter is the interface that wraps the basic DeleteBucketObjects method. -type S3BucketObjectsDeleter interface { - DeleteS3BucketObjects(ctx context.Context, input *S3BucketObjectsDeleterInput) (*S3BucketObjectsDeleterOutput, error) +// S3ObjectsDeleter is the interface that wraps the basic DeleteBucketObjects method. +type S3ObjectsDeleter interface { + DeleteS3Objects(ctx context.Context, input *S3ObjectsDeleterInput) (*S3ObjectsDeleterOutput, error) } -// S3BucketObjectsListerInput is the input of the ListBucketObjects method. -type S3BucketObjectsListerInput struct { +// S3ObjectsListerInput is the input of the ListBucketObjects method. +type S3ObjectsListerInput struct { // Bucket is the name of the bucket to list. Bucket model.Bucket } -// S3BucketObjectsListerOutput is the output of the ListBucketObjects method. -type S3BucketObjectsListerOutput struct { +// S3ObjectsListerOutput is the output of the ListBucketObjects method. +type S3ObjectsListerOutput struct { // Objects is the list of the objects. Objects model.S3ObjectIdentifierSets } -// S3BucketObjectsLister is the interface that wraps the basic ListBucketObjects method. -type S3BucketObjectsLister interface { - ListS3BucketObjects(ctx context.Context, input *S3BucketObjectsListerInput) (*S3BucketObjectsListerOutput, error) +// S3ObjectsLister is the interface that wraps the basic ListBucketObjects method. +type S3ObjectsLister interface { + ListS3Objects(ctx context.Context, input *S3ObjectsListerInput) (*S3ObjectsListerOutput, error) } -// S3BucketObjectDownloaderInput is the input of the GetBucketObject method. -type S3BucketObjectDownloaderInput struct { +// S3ObjectDownloaderInput is the input of the GetBucketObject method. +type S3ObjectDownloaderInput struct { // Bucket is the name of the bucket to get. Bucket model.Bucket - // S3Key is the key of the object to get. - S3Key model.S3Key + // Key is the key of the object to get. + Key model.S3Key } -// S3BucketObjectDownloaderOutput is the output of the GetBucketObject method. -type S3BucketObjectDownloaderOutput struct { - // S3Object is the object. +// S3ObjectDownloaderOutput is the output of the GetBucketObject method. +type S3ObjectDownloaderOutput struct { + // Bucket is the name of the bucket that you want to download. + Bucket model.Bucket + // Key is the S3 key. + Key model.S3Key + // ContentType is the content type of the downloaded file. + ContentType string + // ContentLength is the content length of the downloaded file. + ContentLength int64 + // S3Object is the downloaded object. S3Object *model.S3Object } -// S3BucketObjectDownloader is the interface that wraps the basic GetBucketObject method. -type S3BucketObjectDownloader interface { - DownloadS3BucketObject(ctx context.Context, input *S3BucketObjectDownloaderInput) (*S3BucketObjectDownloaderOutput, error) +// S3ObjectDownloader is the interface that wraps the basic GetBucketObject method. +type S3ObjectDownloader interface { + DownloadS3Object(ctx context.Context, input *S3ObjectDownloaderInput) (*S3ObjectDownloaderOutput, error) } -// S3BucketObjectUploaderInput is the input of the PutBucketObject method. -type S3BucketObjectUploaderInput struct { +// S3ObjectUploaderInput is the input of the PutBucketObject method. +type S3ObjectUploaderInput struct { // Bucket is the name of the bucket to put. Bucket model.Bucket // Region is the region of the bucket that you want to put. @@ -135,15 +143,15 @@ type S3BucketObjectUploaderInput struct { S3Object *model.S3Object } -// S3BucketObjectUploaderOutput is the output of the PutBucketObject method. -type S3BucketObjectUploaderOutput struct { +// S3ObjectUploaderOutput is the output of the PutBucketObject method. +type S3ObjectUploaderOutput struct { // ContentType is the content type of the object. ContentType string // ContentLength is the size of the object. ContentLength int64 } -// S3BucketObjectUploader is the interface that wraps the basic PutBucketObject method. -type S3BucketObjectUploader interface { - UploadS3BucketObject(ctx context.Context, input *S3BucketObjectUploaderInput) (*S3BucketObjectUploaderOutput, error) +// S3ObjectUploader is the interface that wraps the basic PutBucketObject method. +type S3ObjectUploader interface { + UploadS3Object(ctx context.Context, input *S3ObjectUploaderInput) (*S3ObjectUploaderOutput, error) } diff --git a/app/external/s3.go b/app/external/s3.go index 9769d3d..505a587 100644 --- a/app/external/s3.go +++ b/app/external/s3.go @@ -175,28 +175,28 @@ func (c *S3BucketDeleter) DeleteS3Bucket(ctx context.Context, input *service.S3B return &service.S3BucketDeleterOutput{}, nil } -// S3BucketObjectsDeleter implements the S3BucketObjectsDeleter interface. -type S3BucketObjectsDeleter struct { +// S3ObjectsDeleter implements the S3ObjectsDeleter interface. +type S3ObjectsDeleter struct { client *s3.Client } -// S3BucketObjectsDeleterSet is a provider set for S3BucketObjectsDeleter. +// S3ObjectsDeleterSet is a provider set for S3ObjectsDeleter. // //nolint:gochecknoglobals -var S3BucketObjectsDeleterSet = wire.NewSet( - NewS3BucketObjectsDeleter, - wire.Bind(new(service.S3BucketObjectsDeleter), new(*S3BucketObjectsDeleter)), +var S3ObjectsDeleterSet = wire.NewSet( + NewS3ObjectsDeleter, + wire.Bind(new(service.S3ObjectsDeleter), new(*S3ObjectsDeleter)), ) -var _ service.S3BucketObjectsDeleter = (*S3BucketObjectsDeleter)(nil) +var _ service.S3ObjectsDeleter = (*S3ObjectsDeleter)(nil) -// NewS3BucketObjectsDeleter creates a new S3BucketObjectsDeleter. -func NewS3BucketObjectsDeleter(client *s3.Client) *S3BucketObjectsDeleter { - return &S3BucketObjectsDeleter{client: client} +// NewS3ObjectsDeleter creates a new S3ObjectsDeleter. +func NewS3ObjectsDeleter(client *s3.Client) *S3ObjectsDeleter { + return &S3ObjectsDeleter{client: client} } -// DeleteS3BucketObjects deletes the objects in the bucket. -func (c *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, input *service.S3BucketObjectsDeleterInput) (*service.S3BucketObjectsDeleterOutput, error) { +// DeleteS3Objects deletes the objects in the bucket. +func (c *S3ObjectsDeleter) DeleteS3Objects(ctx context.Context, input *service.S3ObjectsDeleterInput) (*service.S3ObjectsDeleterOutput, error) { optFn := func(o *s3.Options) { o.Retryer = NewRetryer(func(err error) bool { return strings.Contains(err.Error(), "api error SlowDown") @@ -217,31 +217,31 @@ func (c *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, inpu ); err != nil { return nil, err } - return &service.S3BucketObjectsDeleterOutput{}, nil + return &service.S3ObjectsDeleterOutput{}, nil } -// S3BucketObjectsLister implements the S3BucketObjectsLister interface. -type S3BucketObjectsLister struct { +// S3ObjectsLister implements the S3ObjectsLister interface. +type S3ObjectsLister struct { client *s3.Client } -// S3BucketObjectsListerSet is a provider set for S3BucketObjectsLister. +// S3ObjectsListerSet is a provider set for S3ObjectsLister. // //nolint:gochecknoglobals -var S3BucketObjectsListerSet = wire.NewSet( - NewS3BucketObjectsLister, - wire.Bind(new(service.S3BucketObjectsLister), new(*S3BucketObjectsLister)), +var S3ObjectsListerSet = wire.NewSet( + NewS3ObjectsLister, + wire.Bind(new(service.S3ObjectsLister), new(*S3ObjectsLister)), ) -var _ service.S3BucketObjectsLister = (*S3BucketObjectsLister)(nil) +var _ service.S3ObjectsLister = (*S3ObjectsLister)(nil) -// NewS3BucketObjectsLister creates a new S3BucketObjectsLister. -func NewS3BucketObjectsLister(client *s3.Client) *S3BucketObjectsLister { - return &S3BucketObjectsLister{client: client} +// NewS3ObjectsLister creates a new S3ObjectsLister. +func NewS3ObjectsLister(client *s3.Client) *S3ObjectsLister { + return &S3ObjectsLister{client: client} } -// ListS3BucketObjects lists the objects in the bucket. -func (c *S3BucketObjectsLister) ListS3BucketObjects(ctx context.Context, input *service.S3BucketObjectsListerInput) (*service.S3BucketObjectsListerOutput, error) { +// ListS3Objects lists the objects in the bucket. +func (c *S3ObjectsLister) ListS3Objects(ctx context.Context, input *service.S3ObjectsListerInput) (*service.S3ObjectsListerOutput, error) { var objects model.S3ObjectIdentifierSets in := &s3.ListObjectsV2Input{ Bucket: aws.String(input.Bucket.String()), @@ -264,34 +264,34 @@ func (c *S3BucketObjectsLister) ListS3BucketObjects(ctx context.Context, input * } in.ContinuationToken = output.NextContinuationToken } - return &service.S3BucketObjectsListerOutput{Objects: objects}, nil + return &service.S3ObjectsListerOutput{Objects: objects}, nil } -// S3BucketObjectDownloader implements the S3BucketObjectDownloader interface. -type S3BucketObjectDownloader struct { +// S3ObjectDownloader implements the S3ObjectDownloader interface. +type S3ObjectDownloader struct { client *s3.Client } -// S3BucketObjectDownloaderSet is a provider set for S3BucketObjectGetter. +// S3ObjectDownloaderSet is a provider set for S3ObjectGetter. // //nolint:gochecknoglobals -var S3BucketObjectDownloaderSet = wire.NewSet( - NewS3BucketObjectDownloader, - wire.Bind(new(service.S3BucketObjectDownloader), new(*S3BucketObjectDownloader)), +var S3ObjectDownloaderSet = wire.NewSet( + NewS3ObjectDownloader, + wire.Bind(new(service.S3ObjectDownloader), new(*S3ObjectDownloader)), ) -var _ service.S3BucketObjectDownloader = (*S3BucketObjectDownloader)(nil) +var _ service.S3ObjectDownloader = (*S3ObjectDownloader)(nil) -// NewS3BucketObjectDownloader creates a new S3BucketObjectGetter. -func NewS3BucketObjectDownloader(client *s3.Client) *S3BucketObjectDownloader { - return &S3BucketObjectDownloader{client: client} +// NewS3ObjectDownloader creates a new S3ObjectGetter. +func NewS3ObjectDownloader(client *s3.Client) *S3ObjectDownloader { + return &S3ObjectDownloader{client: client} } -// DownloadS3BucketObject gets the object in the bucket. -func (c *S3BucketObjectDownloader) DownloadS3BucketObject(ctx context.Context, input *service.S3BucketObjectDownloaderInput) (*service.S3BucketObjectDownloaderOutput, error) { +// DownloadS3Object gets the object in the bucket. +func (c *S3ObjectDownloader) DownloadS3Object(ctx context.Context, input *service.S3ObjectDownloaderInput) (*service.S3ObjectDownloaderOutput, error) { out, err := c.client.GetObject(ctx, &s3.GetObjectInput{ Bucket: aws.String(input.Bucket.String()), - Key: aws.String(input.S3Key.String()), + Key: aws.String(input.Key.String()), }) if err != nil { return nil, err @@ -310,33 +310,37 @@ func (c *S3BucketObjectDownloader) DownloadS3BucketObject(ctx context.Context, i return nil, err } - return &service.S3BucketObjectDownloaderOutput{ - S3Object: model.NewS3Object(b), + return &service.S3ObjectDownloaderOutput{ + Bucket: input.Bucket, + Key: input.Key, + ContentType: aws.ToString(out.ContentType), + ContentLength: aws.ToInt64(out.ContentLength), + S3Object: model.NewS3Object(b), }, nil } -// S3BucketObjectUploader implements the S3BucketObjectUploader interface. -type S3BucketObjectUploader struct { +// S3ObjectUploader implements the S3ObjectUploader interface. +type S3ObjectUploader struct { client *s3.Client } -// S3BucketObjectUploaderSet is a provider set for S3BucketObjectUploader. +// S3ObjectUploaderSet is a provider set for S3ObjectUploader. // //nolint:gochecknoglobals -var S3BucketObjectUploaderSet = wire.NewSet( - NewS3BucketObjectUploader, - wire.Bind(new(service.S3BucketObjectUploader), new(*S3BucketObjectUploader)), +var S3ObjectUploaderSet = wire.NewSet( + NewS3ObjectUploader, + wire.Bind(new(service.S3ObjectUploader), new(*S3ObjectUploader)), ) -var _ service.S3BucketObjectUploader = (*S3BucketObjectUploader)(nil) +var _ service.S3ObjectUploader = (*S3ObjectUploader)(nil) -// NewS3BucketObjectUploader creates a new S3BucketObjectUploader. -func NewS3BucketObjectUploader(client *s3.Client) *S3BucketObjectUploader { - return &S3BucketObjectUploader{client: client} +// NewS3ObjectUploader creates a new S3ObjectUploader. +func NewS3ObjectUploader(client *s3.Client) *S3ObjectUploader { + return &S3ObjectUploader{client: client} } -// UploadS3BucketObject puts the object in the bucket. -func (c *S3BucketObjectUploader) UploadS3BucketObject(ctx context.Context, input *service.S3BucketObjectUploaderInput) (*service.S3BucketObjectUploaderOutput, error) { +// UploadS3Object puts the object in the bucket. +func (c *S3ObjectUploader) UploadS3Object(ctx context.Context, input *service.S3ObjectUploaderInput) (*service.S3ObjectUploaderOutput, error) { _, err := c.client.PutObject(ctx, &s3.PutObjectInput{ Bucket: aws.String(input.Bucket.String()), Key: aws.String(input.S3Key.String()), @@ -347,7 +351,7 @@ func (c *S3BucketObjectUploader) UploadS3BucketObject(ctx context.Context, input if err != nil { return nil, err } - return &service.S3BucketObjectUploaderOutput{ + return &service.S3ObjectUploaderOutput{ ContentType: input.S3Object.ContentType(), ContentLength: input.S3Object.ContentLength(), }, nil diff --git a/app/interactor/s3.go b/app/interactor/s3.go index 4735382..ef74235 100644 --- a/app/interactor/s3.go +++ b/app/interactor/s3.go @@ -98,71 +98,71 @@ func (s *S3BucketLister) ListS3Buckets(ctx context.Context, _ *usecase.S3BucketL }, nil } -// S3BucketObjectsLister implements the S3BucketObjectsLister interface. -type S3BucketObjectsLister struct { - service.S3BucketObjectsLister +// S3ObjectsLister implements the S3ObjectsLister interface. +type S3ObjectsLister struct { + service.S3ObjectsLister } -// S3BucketObjectsListerSet is a provider set for S3BucketObjectsLister. +// S3ObjectsListerSet is a provider set for S3ObjectsLister. // //nolint:gochecknoglobals -var S3BucketObjectsListerSet = wire.NewSet( - NewS3BucketObjectsLister, - wire.Bind(new(usecase.S3BucketObjectsLister), new(*S3BucketObjectsLister)), +var S3ObjectsListerSet = wire.NewSet( + NewS3ObjectsLister, + wire.Bind(new(usecase.S3ObjectsLister), new(*S3ObjectsLister)), ) -var _ usecase.S3BucketObjectsLister = (*S3BucketObjectsLister)(nil) +var _ usecase.S3ObjectsLister = (*S3ObjectsLister)(nil) -// NewS3BucketObjectsLister creates a new S3BucketObjectsLister. -func NewS3BucketObjectsLister(l service.S3BucketObjectsLister) *S3BucketObjectsLister { - return &S3BucketObjectsLister{ - S3BucketObjectsLister: l, +// NewS3ObjectsLister creates a new S3ObjectsLister. +func NewS3ObjectsLister(l service.S3ObjectsLister) *S3ObjectsLister { + return &S3ObjectsLister{ + S3ObjectsLister: l, } } -// ListS3BucketObjects lists the objects in the S3 bucket. -func (s *S3BucketObjectsLister) ListS3BucketObjects(ctx context.Context, input *usecase.S3BucketObjectsListerInput) (*usecase.S3BucketObjectsListerOutput, error) { +// ListS3Objects lists the objects in the S3 bucket. +func (s *S3ObjectsLister) ListS3Objects(ctx context.Context, input *usecase.S3ObjectsListerInput) (*usecase.S3ObjectsListerOutput, error) { if err := input.Bucket.Validate(); err != nil { return nil, err } - out, err := s.S3BucketObjectsLister.ListS3BucketObjects(ctx, &service.S3BucketObjectsListerInput{ + out, err := s.S3ObjectsLister.ListS3Objects(ctx, &service.S3ObjectsListerInput{ Bucket: input.Bucket, }) if err != nil { return nil, err } - return &usecase.S3BucketObjectsListerOutput{ + return &usecase.S3ObjectsListerOutput{ Objects: out.Objects, }, nil } -// S3BucketObjectsDeleter implements the S3BucketObjectsDeleter interface. -type S3BucketObjectsDeleter struct { - service.S3BucketObjectsDeleter +// S3ObjectsDeleter implements the S3ObjectsDeleter interface. +type S3ObjectsDeleter struct { + service.S3ObjectsDeleter service.S3BucketLocationGetter } -// S3BucketObjectsDeleterSet is a provider set for S3BucketObjectsDeleter. +// S3ObjectsDeleterSet is a provider set for S3ObjectsDeleter. // //nolint:gochecknoglobals -var S3BucketObjectsDeleterSet = wire.NewSet( - NewS3BucketObjectsDeleter, - wire.Bind(new(usecase.S3BucketObjectsDeleter), new(*S3BucketObjectsDeleter)), +var S3ObjectsDeleterSet = wire.NewSet( + NewS3ObjectsDeleter, + wire.Bind(new(usecase.S3ObjectsDeleter), new(*S3ObjectsDeleter)), ) -var _ usecase.S3BucketObjectsDeleter = (*S3BucketObjectsDeleter)(nil) +var _ usecase.S3ObjectsDeleter = (*S3ObjectsDeleter)(nil) -// NewS3BucketObjectsDeleter creates a new S3BucketObjectsDeleter. -func NewS3BucketObjectsDeleter(d service.S3BucketObjectsDeleter, l service.S3BucketLocationGetter) *S3BucketObjectsDeleter { - return &S3BucketObjectsDeleter{ - S3BucketObjectsDeleter: d, +// NewS3ObjectsDeleter creates a new S3ObjectsDeleter. +func NewS3ObjectsDeleter(d service.S3ObjectsDeleter, l service.S3BucketLocationGetter) *S3ObjectsDeleter { + return &S3ObjectsDeleter{ + S3ObjectsDeleter: d, S3BucketLocationGetter: l, } } -// DeleteS3BucketObjects deletes the objects in the bucket. -func (s *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, input *usecase.S3BucketObjectsDeleterInput) (*usecase.S3BucketObjectsDeleterOutput, error) { +// DeleteS3Objects deletes the objects in the bucket. +func (s *S3ObjectsDeleter) DeleteS3Objects(ctx context.Context, input *usecase.S3ObjectsDeleterInput) (*usecase.S3ObjectsDeleterOutput, error) { if err := input.Bucket.Validate(); err != nil { return nil, err } @@ -174,7 +174,7 @@ func (s *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, inpu return nil, err } - _, err = s.S3BucketObjectsDeleter.DeleteS3BucketObjects(ctx, &service.S3BucketObjectsDeleterInput{ + _, err = s.S3ObjectsDeleter.DeleteS3Objects(ctx, &service.S3ObjectsDeleterInput{ Bucket: input.Bucket, Region: location.Region, S3ObjectSets: input.S3ObjectSets, @@ -182,7 +182,7 @@ func (s *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, inpu if err != nil { return nil, err } - return &usecase.S3BucketObjectsDeleterOutput{}, nil + return &usecase.S3ObjectsDeleterOutput{}, nil } // S3BucketDeleterSet is a provider set for S3BucketDeleter. @@ -252,7 +252,7 @@ type FileUploader struct { // FileUploaderOptions is an option struct for FileUploader. type FileUploaderOptions struct { - service.S3BucketObjectUploader + service.S3ObjectUploader } // NewFileUploader returns a new FileUploader struct. @@ -264,7 +264,7 @@ func NewFileUploader(opts *FileUploaderOptions) *FileUploader { // UploadFile uploads a file to external storage. func (u *FileUploader) UploadFile(ctx context.Context, input *usecase.UploadFileInput) (*usecase.UploadFileOutput, error) { - output, err := u.opts.S3BucketObjectUploader.UploadS3BucketObject(ctx, &service.S3BucketObjectUploaderInput{ + output, err := u.opts.S3ObjectUploader.UploadS3Object(ctx, &service.S3ObjectUploaderInput{ Bucket: input.Bucket, Region: input.Region, S3Key: input.Key, @@ -344,3 +344,47 @@ func (s *S3BucketPolicySetter) SetS3BucketPolicy(ctx context.Context, input *use } return &usecase.S3BucketPolicySetterOutput{}, nil } + +// S3ObjectDownloaderSet is a provider set for S3ObjectDownloader. +// +//nolint:gochecknoglobals +var S3ObjectDownloaderSet = wire.NewSet( + NewS3ObjectDownloader, + wire.Bind(new(usecase.S3ObjectDownloader), new(*S3ObjectDownloader)), +) + +// S3ObjectDownloader is an implementation for S3ObjectDownloader. +type S3ObjectDownloader struct { + service.S3ObjectDownloader +} + +var _ usecase.S3ObjectDownloader = (*S3ObjectDownloader)(nil) + +// NewS3ObjectDownloader returns a new S3ObjectDownloader struct. +func NewS3ObjectDownloader(d service.S3ObjectDownloader) *S3ObjectDownloader { + return &S3ObjectDownloader{ + S3ObjectDownloader: d, + } +} + +// DownloadS3Object downloads an object from S3. +func (s *S3ObjectDownloader) DownloadS3Object(ctx context.Context, input *usecase.S3ObjectDownloaderInput) (*usecase.S3ObjectDownloaderOutput, error) { + if err := input.Bucket.Validate(); err != nil { + return nil, err + } + + out, err := s.S3ObjectDownloader.DownloadS3Object(ctx, &service.S3ObjectDownloaderInput{ + Bucket: input.Bucket, + Key: input.Key, + }) + if err != nil { + return nil, err + } + return &usecase.S3ObjectDownloaderOutput{ + Bucket: out.Bucket, + Key: out.Key, + ContentType: out.ContentType, + ContentLength: out.ContentLength, + S3Object: out.S3Object, + }, nil +} diff --git a/app/usecase/s3.go b/app/usecase/s3.go index cf8a5e5..8deebc2 100644 --- a/app/usecase/s3.go +++ b/app/usecase/s3.go @@ -37,21 +37,21 @@ type S3BucketLister interface { ListS3Buckets(ctx context.Context, input *S3BucketListerInput) (*S3BucketListerOutput, error) } -// S3BucketObjectsListerInput is the input of the ListObjects method. -type S3BucketObjectsListerInput struct { +// S3ObjectsListerInput is the input of the ListObjects method. +type S3ObjectsListerInput struct { // Bucket is the name of the bucket that you want to list objects. Bucket model.Bucket } -// S3BucketObjectsListerOutput is the output of the ListObjects method. -type S3BucketObjectsListerOutput struct { +// S3ObjectsListerOutput is the output of the ListObjects method. +type S3ObjectsListerOutput struct { // Objects is the list of the objects. Objects model.S3ObjectIdentifierSets } -// S3BucketObjectsLister is the interface that wraps the basic ListObjects method. -type S3BucketObjectsLister interface { - ListS3BucketObjects(ctx context.Context, input *S3BucketObjectsListerInput) (*S3BucketObjectsListerOutput, error) +// S3ObjectsLister is the interface that wraps the basic ListObjects method. +type S3ObjectsLister interface { + ListS3Objects(ctx context.Context, input *S3ObjectsListerInput) (*S3ObjectsListerOutput, error) } // S3BucketDeleterInput is the input of the DeleteBucket method. @@ -68,20 +68,47 @@ type S3BucketDeleter interface { DeleteS3Bucket(ctx context.Context, input *S3BucketDeleterInput) (*S3BucketDeleterOutput, error) } -// S3BucketObjectsDeleterInput is the input of the DeleteObjects method. -type S3BucketObjectsDeleterInput struct { +// S3ObjectsDeleterInput is the input of the DeleteObjects method. +type S3ObjectsDeleterInput struct { // Bucket is the name of the bucket that you want to delete. Bucket model.Bucket // S3ObjectSets is the list of the objects to delete. S3ObjectSets model.S3ObjectIdentifierSets } -// S3BucketObjectsDeleterOutput is the output of the DeleteObjects method. -type S3BucketObjectsDeleterOutput struct{} +// S3ObjectsDeleterOutput is the output of the DeleteObjects method. +type S3ObjectsDeleterOutput struct{} -// S3BucketObjectsDeleter is the interface that wraps the basic DeleteObjects method. -type S3BucketObjectsDeleter interface { - DeleteS3BucketObjects(ctx context.Context, input *S3BucketObjectsDeleterInput) (*S3BucketObjectsDeleterOutput, error) +// S3ObjectsDeleter is the interface that wraps the basic DeleteObjects method. +type S3ObjectsDeleter interface { + DeleteS3Objects(ctx context.Context, input *S3ObjectsDeleterInput) (*S3ObjectsDeleterOutput, error) +} + +// S3ObjectDownLoaderInput is the input of the DownloadObject method. +type S3ObjectDownloaderInput struct { + // Bucket is the name of the bucket that you want to download. + Bucket model.Bucket + // Key is the S3 key. + Key model.S3Key +} + +// S3ObjectDownLoaderOutput is the output of the DownloadObject method. +type S3ObjectDownloaderOutput struct { + // Bucket is the name of the bucket that you want to download. + Bucket model.Bucket + // Key is the S3 key. + Key model.S3Key + // ContentType is the content type of the downloaded file. + ContentType string + // ContentLength is the content length of the downloaded file. + ContentLength int64 + // S3Object is the downloaded object. + S3Object *model.S3Object +} + +// S3ObjectDownloader is the interface that wraps the basic DownloadObject method. +type S3ObjectDownloader interface { + DownloadS3Object(ctx context.Context, input *S3ObjectDownloaderInput) (*S3ObjectDownloaderOutput, error) } // FileUploader is an interface for uploading files to external storage. diff --git a/cmd/subcmd/s3hub/ls.go b/cmd/subcmd/s3hub/ls.go index 10d5683..806b57a 100644 --- a/cmd/subcmd/s3hub/ls.go +++ b/cmd/subcmd/s3hub/ls.go @@ -105,7 +105,7 @@ func (l *lsCmd) printObject() error { return fmt.Errorf("bucket not found: %s", color.YellowString("%s", l.bucket)) } - listS3Objects, err := l.s3hub.S3BucketObjectsLister.ListS3BucketObjects(l.ctx, &usecase.S3BucketObjectsListerInput{ + listS3Objects, err := l.s3hub.S3ObjectsLister.ListS3Objects(l.ctx, &usecase.S3ObjectsListerInput{ Bucket: l.bucket, }) if err != nil { diff --git a/cmd/subcmd/s3hub/rm.go b/cmd/subcmd/s3hub/rm.go index a1aa8c2..676b042 100644 --- a/cmd/subcmd/s3hub/rm.go +++ b/cmd/subcmd/s3hub/rm.go @@ -133,7 +133,7 @@ func (r *rmCmd) remove(bucket model.Bucket, key model.S3Key) error { // removeObject removes a object in bucket. func (r *rmCmd) removeObject(bucket model.Bucket, key model.S3Key) error { - if _, err := r.S3App.S3BucketObjectsDeleter.DeleteS3BucketObjects(r.ctx, &usecase.S3BucketObjectsDeleterInput{ + if _, err := r.S3App.S3ObjectsDeleter.DeleteS3Objects(r.ctx, &usecase.S3ObjectsDeleterInput{ Bucket: bucket, S3ObjectSets: model.S3ObjectIdentifierSets{ model.S3ObjectIdentifier{ @@ -148,7 +148,7 @@ func (r *rmCmd) removeObject(bucket model.Bucket, key model.S3Key) error { // removeObjects removes all objects in bucket. func (r *rmCmd) removeObjects(bucket model.Bucket) error { - output, err := r.S3App.S3BucketObjectsLister.ListS3BucketObjects(r.ctx, &usecase.S3BucketObjectsListerInput{ + output, err := r.S3App.S3ObjectsLister.ListS3Objects(r.ctx, &usecase.S3ObjectsListerInput{ Bucket: bucket, }) if err != nil { @@ -173,7 +173,7 @@ func (r *rmCmd) removeObjects(bucket model.Bucket) error { eg.Go(func() error { defer sem.Release(1) - if _, err := r.S3App.S3BucketObjectsDeleter.DeleteS3BucketObjects(ctx, &usecase.S3BucketObjectsDeleterInput{ + if _, err := r.S3App.S3ObjectsDeleter.DeleteS3Objects(ctx, &usecase.S3ObjectsDeleterInput{ Bucket: bucket, S3ObjectSets: chunk, }); err != nil {