s3: Check if directory exists during Mkdir
If you dont have privs to create a bucket in S3 but it exists, don't fail with an auth error, but detect that the mkdir was not needed and return successfully.
This commit is contained in:
parent
6b6b43402b
commit
694d390710
1 changed files with 22 additions and 1 deletions
23
s3/s3.go
23
s3/s3.go
|
@ -505,8 +505,29 @@ func (f *Fs) Put(in io.Reader, src fs.ObjectInfo) (fs.Object, error) {
|
||||||
return fs, fs.Update(in, src)
|
return fs, fs.Update(in, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the bucket exists
|
||||||
|
func (f *Fs) dirExists() (bool, error) {
|
||||||
|
req := s3.HeadBucketInput{
|
||||||
|
Bucket: &f.bucket,
|
||||||
|
}
|
||||||
|
_, err := f.c.HeadBucket(&req)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if err, ok := err.(awserr.RequestFailure); ok {
|
||||||
|
if err.StatusCode() == http.StatusNotFound {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
// Mkdir creates the bucket if it doesn't exist
|
// Mkdir creates the bucket if it doesn't exist
|
||||||
func (f *Fs) Mkdir() error {
|
func (f *Fs) Mkdir() error {
|
||||||
|
exists, err := f.dirExists()
|
||||||
|
if err != nil || exists {
|
||||||
|
return err
|
||||||
|
}
|
||||||
req := s3.CreateBucketInput{
|
req := s3.CreateBucketInput{
|
||||||
Bucket: &f.bucket,
|
Bucket: &f.bucket,
|
||||||
ACL: &f.perm,
|
ACL: &f.perm,
|
||||||
|
@ -516,7 +537,7 @@ func (f *Fs) Mkdir() error {
|
||||||
LocationConstraint: &f.locationConstraint,
|
LocationConstraint: &f.locationConstraint,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, err := f.c.CreateBucket(&req)
|
_, err = f.c.CreateBucket(&req)
|
||||||
if err, ok := err.(awserr.Error); ok {
|
if err, ok := err.(awserr.Error); ok {
|
||||||
if err.Code() == "BucketAlreadyOwnedByYou" {
|
if err.Code() == "BucketAlreadyOwnedByYou" {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue