Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

s3: report the real storage class when an object needs restoring

Dirk Petersen6ed7b9d7753f
diff --git a/backend/s3/s3.go b/backend/s3/s3.go
index 36c1348bc..d35abdc43 100644
--- a/backend/s3/s3.go
+++ b/backend/s3/s3.go
@@ -4529,7 +4529,16 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
})
if awsError, ok := errors.AsType[smithy.APIError](err); ok {
if awsError.ErrorCode() == "InvalidObjectState" {
- return nil, fmt.Errorf("Object in GLACIER, restore first: bucket=%q, key=%q", bucket, bucketPath)
+ storageClass := "GLACIER"
+ if stateErr, ok := errors.AsType[*types.InvalidObjectState](err); ok && stateErr.StorageClass != "" {
+ storageClass = string(stateErr.StorageClass)
+ if stateErr.AccessTier != "" {
+ storageClass += " (" + string(stateErr.AccessTier) + " tier)"
+ }
+ } else if o.storageClass != nil && *o.storageClass != "" {
+ storageClass = *o.storageClass
+ }
+ return nil, fmt.Errorf("Object in %s, restore first: bucket=%q, key=%q", storageClass, bucket, bucketPath)
}
}
if err != nil {
diff --git a/backend/s3/s3_test.go b/backend/s3/s3_test.go
index 9c6f83509..076a99036 100644
--- a/backend/s3/s3_test.go
+++ b/backend/s3/s3_test.go
@@ -3,8 +3,10 @@ package s3
import (
"context"
+ "io"
"net/http"
"net/http/httptest"
+ "path"
"testing"
"time"
@@ -498,3 +500,61 @@ func TestParseRetainUntilDate(t *testing.T) {
})
}
}
+
+func TestOpenRestoreFirstError(t *testing.T) {
+ errorBodies := map[string]string{
+ "DeepArchive": `<Error><Code>InvalidObjectState</Code><StorageClass>DEEP_ARCHIVE</StorageClass></Error>`,
+ "IntelligentTiering": `<Error><Code>InvalidObjectState</Code><StorageClass>INTELLIGENT_TIERING</StorageClass><AccessTier>DEEP_ARCHIVE_ACCESS</AccessTier></Error>`,
+ "FromObject": `<Error><Code>InvalidObjectState</Code></Error>`,
+ "Unknown": `<Error><Code>InvalidObjectState</Code></Error>`,
+ }
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusForbidden)
+ _, _ = io.WriteString(w, errorBodies[path.Base(r.URL.Path)])
+ }))
+ defer server.Close()
+
+ ctx, opt, client := SetupS3Test(t)
+ opt.Endpoint = server.URL
+ opt.ForcePathStyle = true
+ opt.Region = "us-east-1"
+ opt.AccessKeyID = "test-access-key"
+ opt.SecretAccessKey = "test-secret-key"
+ c, _, err := s3Connection(ctx, opt, client)
+ require.NoError(t, err)
+ f := &Fs{
+ name: "s3test",
+ opt: *opt,
+ ctx: ctx,
+ c: c,
+ pacer: fs.NewPacer(ctx, pacer.NewS3(pacer.MinSleep(minSleep))),
+ cache: bucket.NewCache(),
+ }
+ f.setRoot("bucket")
+
+ for _, test := range []struct {
+ name string
+ storageClass *string
+ want string
+ }{{
+ name: "DeepArchive",
+ want: "Object in DEEP_ARCHIVE, restore first",
+ }, {
+ name: "IntelligentTiering",
+ want: "Object in INTELLIGENT_TIERING (DEEP_ARCHIVE_ACCESS tier), restore first",
+ }, {
+ name: "FromObject",
+ storageClass: aws.String("DEEP_ARCHIVE"),
+ want: "Object in DEEP_ARCHIVE, restore first",
+ }, {
+ name: "Unknown",
+ want: "Object in GLACIER, restore first",
+ }} {
+ t.Run(test.name, func(t *testing.T) {
+ o := &Object{fs: f, remote: test.name, storageClass: test.storageClass}
+ _, err := o.Open(ctx)
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), test.want)
+ })
+ }
+}
diff --git a/docs/content/s3.md b/docs/content/s3.md
index 9fdf5d864..5c297d43a 100644
--- a/docs/content/s3.md
+++ b/docs/content/s3.md
@@ -904,6 +904,9 @@ tries to access data from the glacier storage class you will see an error like b
2017/09/11 19:07:43 Failed to sync: failed to open source object: Object in GLACIER, restore first: path/to/file
```
+The message shows the object's actual storage class, for example
+`DEEP_ARCHIVE`.
+
In this case you need to [restore](http://docs.aws.amazon.com/AmazonS3/latest/user-guide/restore-archived-objects.html)
the object(s) in question before accessing object contents.
The [restore](#restore) section below shows how to do this with rclone.