2015-09-22 19:47:16 +02:00
// Package s3 provides an interface to Amazon S3 oject storage
2013-06-27 21:13:07 +02:00
package s3
2013-01-08 19:53:35 +01:00
2013-01-23 23:43:20 +01:00
// FIXME need to prevent anything but ListDir working for s3://
2014-12-23 13:09:02 +01:00
/ *
Progress of port to aws - sdk
* Don ' t really need o . meta at all ?
What happens if you CTRL - C a multipart upload
* get an incomplete upload
* disappears when you delete the bucket
* /
2013-01-08 19:53:35 +01:00
import (
2019-06-17 10:34:30 +02:00
"context"
2018-01-06 15:30:10 +01:00
"encoding/base64"
"encoding/hex"
2019-09-16 21:25:55 +02:00
"encoding/xml"
2013-01-08 19:53:35 +01:00
"fmt"
"io"
2016-02-01 14:11:27 +01:00
"net/http"
2019-07-23 13:24:10 +02:00
"net/url"
2013-01-08 19:53:35 +01:00
"path"
"regexp"
2019-10-04 17:49:06 +02:00
"strconv"
2013-01-08 19:53:35 +01:00
"strings"
2019-11-06 11:41:03 +01:00
"sync"
2013-01-08 19:53:35 +01:00
"time"
2014-12-23 13:09:02 +01:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
2015-08-28 09:47:41 +02:00
"github.com/aws/aws-sdk-go/aws/corehandlers"
2014-12-23 13:09:02 +01:00
"github.com/aws/aws-sdk-go/aws/credentials"
2016-02-01 14:11:27 +01:00
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2017-11-22 22:21:36 +01:00
"github.com/aws/aws-sdk-go/aws/defaults"
2016-02-01 14:11:27 +01:00
"github.com/aws/aws-sdk-go/aws/ec2metadata"
2015-08-28 09:47:41 +02:00
"github.com/aws/aws-sdk-go/aws/request"
2015-10-30 12:50:45 +01:00
"github.com/aws/aws-sdk-go/aws/session"
2014-12-23 13:09:02 +01:00
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
2014-03-15 17:06:11 +01:00
"github.com/ncw/swift"
2016-06-12 16:06:02 +02:00
"github.com/pkg/errors"
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
2018-11-02 13:15:30 +01:00
"github.com/rclone/rclone/fs/encodings"
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/fs/fshttp"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/walk"
2019-08-09 12:29:36 +02:00
"github.com/rclone/rclone/lib/bucket"
2019-07-28 19:47:38 +02:00
"github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/rest"
2014-03-15 17:06:11 +01:00
)
2013-06-27 21:13:07 +02:00
2018-11-02 13:15:30 +01:00
const enc = encodings . S3
2013-06-27 21:13:07 +02:00
// Register with Fs
func init ( ) {
2016-02-18 12:35:25 +01:00
fs . Register ( & fs . RegInfo {
2016-02-15 19:11:53 +01:00
Name : "s3" ,
2019-01-12 17:46:45 +01:00
Description : "Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)" ,
2016-02-15 19:11:53 +01:00
NewFs : NewFs ,
2014-03-15 17:06:11 +01:00
Options : [ ] fs . Option { {
2018-04-13 17:08:00 +02:00
Name : fs . ConfigProvider ,
Help : "Choose your S3 provider." ,
Examples : [ ] fs . OptionExample { {
Value : "AWS" ,
Help : "Amazon Web Services (AWS) S3" ,
2019-01-12 17:46:45 +01:00
} , {
Value : "Alibaba" ,
Help : "Alibaba Cloud Object Storage System (OSS) formerly Aliyun" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "Ceph" ,
Help : "Ceph Object Storage" ,
} , {
Value : "DigitalOcean" ,
Help : "Digital Ocean Spaces" ,
} , {
Value : "Dreamhost" ,
Help : "Dreamhost DreamObjects" ,
} , {
Value : "IBMCOS" ,
Help : "IBM COS S3" ,
} , {
Value : "Minio" ,
Help : "Minio Object Storage" ,
2019-01-12 17:46:45 +01:00
} , {
Value : "Netease" ,
Help : "Netease Object Storage (NOS)" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "Wasabi" ,
Help : "Wasabi Object Storage" ,
} , {
Value : "Other" ,
Help : "Any other S3 compatible provider" ,
} } ,
} , {
2018-05-14 19:06:57 +02:00
Name : "env_auth" ,
Help : "Get AWS credentials from runtime (environment variables or EC2/ECS meta data if no env vars).\nOnly applies if access_key_id and secret_access_key is blank." ,
Default : false ,
2018-04-13 17:08:00 +02:00
Examples : [ ] fs . OptionExample { {
Value : "false" ,
Help : "Enter AWS credentials in the next step" ,
} , {
Value : "true" ,
Help : "Get AWS credentials from the environment (env vars or IAM)" ,
} } ,
} , {
Name : "access_key_id" ,
2018-05-14 19:06:57 +02:00
Help : "AWS Access Key ID.\nLeave blank for anonymous access or runtime credentials." ,
2018-04-13 17:08:00 +02:00
} , {
Name : "secret_access_key" ,
2018-05-14 19:06:57 +02:00
Help : "AWS Secret Access Key (password)\nLeave blank for anonymous access or runtime credentials." ,
2018-04-13 17:08:00 +02:00
} , {
Name : "region" ,
Help : "Region to connect to." ,
Provider : "AWS" ,
Examples : [ ] fs . OptionExample { {
Value : "us-east-1" ,
Help : "The default endpoint - a good choice if you are unsure.\nUS Region, Northern Virginia or Pacific Northwest.\nLeave location constraint empty." ,
} , {
Value : "us-east-2" ,
Help : "US East (Ohio) Region\nNeeds location constraint us-east-2." ,
} , {
Value : "us-west-2" ,
Help : "US West (Oregon) Region\nNeeds location constraint us-west-2." ,
} , {
Value : "us-west-1" ,
Help : "US West (Northern California) Region\nNeeds location constraint us-west-1." ,
} , {
Value : "ca-central-1" ,
Help : "Canada (Central) Region\nNeeds location constraint ca-central-1." ,
} , {
Value : "eu-west-1" ,
Help : "EU (Ireland) Region\nNeeds location constraint EU or eu-west-1." ,
} , {
Value : "eu-west-2" ,
Help : "EU (London) Region\nNeeds location constraint eu-west-2." ,
2019-02-02 20:44:26 +01:00
} , {
Value : "eu-north-1" ,
Help : "EU (Stockholm) Region\nNeeds location constraint eu-north-1." ,
2018-04-13 17:08:00 +02:00
} , {
Value : "eu-central-1" ,
Help : "EU (Frankfurt) Region\nNeeds location constraint eu-central-1." ,
} , {
Value : "ap-southeast-1" ,
Help : "Asia Pacific (Singapore) Region\nNeeds location constraint ap-southeast-1." ,
} , {
Value : "ap-southeast-2" ,
Help : "Asia Pacific (Sydney) Region\nNeeds location constraint ap-southeast-2." ,
} , {
Value : "ap-northeast-1" ,
Help : "Asia Pacific (Tokyo) Region\nNeeds location constraint ap-northeast-1." ,
} , {
Value : "ap-northeast-2" ,
Help : "Asia Pacific (Seoul)\nNeeds location constraint ap-northeast-2." ,
} , {
Value : "ap-south-1" ,
Help : "Asia Pacific (Mumbai)\nNeeds location constraint ap-south-1." ,
2020-01-02 12:10:48 +01:00
} , {
Value : "ap-east-1" ,
Help : "Asia Patific (Hong Kong) Region\nNeeds location constraint ap-east-1." ,
2018-04-13 17:08:00 +02:00
} , {
Value : "sa-east-1" ,
Help : "South America (Sao Paulo) Region\nNeeds location constraint sa-east-1." ,
} } ,
} , {
Name : "region" ,
2018-05-14 19:06:57 +02:00
Help : "Region to connect to.\nLeave blank if you are using an S3 clone and you don't have a region." ,
2019-01-12 17:46:45 +01:00
Provider : "!AWS,Alibaba" ,
2018-04-13 17:08:00 +02:00
Examples : [ ] fs . OptionExample { {
Value : "" ,
Help : "Use this if unsure. Will use v4 signatures and an empty region." ,
} , {
Value : "other-v2-signature" ,
Help : "Use this only if v4 signatures don't work, eg pre Jewel/v10 CEPH." ,
} } ,
} , {
Name : "endpoint" ,
Help : "Endpoint for S3 API.\nLeave blank if using AWS to use the default endpoint for the region." ,
Provider : "AWS" ,
} , {
Name : "endpoint" ,
Help : "Endpoint for IBM COS S3 API.\nSpecify if using an IBM COS On Premise." ,
Provider : "IBMCOS" ,
Examples : [ ] fs . OptionExample { {
Value : "s3-api.us-geo.objectstorage.softlayer.net" ,
Help : "US Cross Region Endpoint" ,
} , {
Value : "s3-api.dal.us-geo.objectstorage.softlayer.net" ,
Help : "US Cross Region Dallas Endpoint" ,
} , {
Value : "s3-api.wdc-us-geo.objectstorage.softlayer.net" ,
Help : "US Cross Region Washington DC Endpoint" ,
} , {
Value : "s3-api.sjc-us-geo.objectstorage.softlayer.net" ,
Help : "US Cross Region San Jose Endpoint" ,
} , {
Value : "s3-api.us-geo.objectstorage.service.networklayer.com" ,
Help : "US Cross Region Private Endpoint" ,
} , {
Value : "s3-api.dal-us-geo.objectstorage.service.networklayer.com" ,
Help : "US Cross Region Dallas Private Endpoint" ,
} , {
Value : "s3-api.wdc-us-geo.objectstorage.service.networklayer.com" ,
Help : "US Cross Region Washington DC Private Endpoint" ,
} , {
Value : "s3-api.sjc-us-geo.objectstorage.service.networklayer.com" ,
Help : "US Cross Region San Jose Private Endpoint" ,
} , {
Value : "s3.us-east.objectstorage.softlayer.net" ,
Help : "US Region East Endpoint" ,
} , {
Value : "s3.us-east.objectstorage.service.networklayer.com" ,
Help : "US Region East Private Endpoint" ,
} , {
Value : "s3.us-south.objectstorage.softlayer.net" ,
Help : "US Region South Endpoint" ,
} , {
Value : "s3.us-south.objectstorage.service.networklayer.com" ,
Help : "US Region South Private Endpoint" ,
} , {
Value : "s3.eu-geo.objectstorage.softlayer.net" ,
Help : "EU Cross Region Endpoint" ,
} , {
Value : "s3.fra-eu-geo.objectstorage.softlayer.net" ,
Help : "EU Cross Region Frankfurt Endpoint" ,
} , {
Value : "s3.mil-eu-geo.objectstorage.softlayer.net" ,
Help : "EU Cross Region Milan Endpoint" ,
} , {
Value : "s3.ams-eu-geo.objectstorage.softlayer.net" ,
Help : "EU Cross Region Amsterdam Endpoint" ,
} , {
Value : "s3.eu-geo.objectstorage.service.networklayer.com" ,
Help : "EU Cross Region Private Endpoint" ,
} , {
Value : "s3.fra-eu-geo.objectstorage.service.networklayer.com" ,
Help : "EU Cross Region Frankfurt Private Endpoint" ,
} , {
Value : "s3.mil-eu-geo.objectstorage.service.networklayer.com" ,
Help : "EU Cross Region Milan Private Endpoint" ,
} , {
Value : "s3.ams-eu-geo.objectstorage.service.networklayer.com" ,
Help : "EU Cross Region Amsterdam Private Endpoint" ,
} , {
Value : "s3.eu-gb.objectstorage.softlayer.net" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Endpoint" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "s3.eu-gb.objectstorage.service.networklayer.com" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Private Endpoint" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "s3.ap-geo.objectstorage.softlayer.net" ,
Help : "APAC Cross Regional Endpoint" ,
} , {
Value : "s3.tok-ap-geo.objectstorage.softlayer.net" ,
Help : "APAC Cross Regional Tokyo Endpoint" ,
} , {
Value : "s3.hkg-ap-geo.objectstorage.softlayer.net" ,
Help : "APAC Cross Regional HongKong Endpoint" ,
} , {
Value : "s3.seo-ap-geo.objectstorage.softlayer.net" ,
Help : "APAC Cross Regional Seoul Endpoint" ,
} , {
Value : "s3.ap-geo.objectstorage.service.networklayer.com" ,
Help : "APAC Cross Regional Private Endpoint" ,
} , {
Value : "s3.tok-ap-geo.objectstorage.service.networklayer.com" ,
Help : "APAC Cross Regional Tokyo Private Endpoint" ,
} , {
Value : "s3.hkg-ap-geo.objectstorage.service.networklayer.com" ,
Help : "APAC Cross Regional HongKong Private Endpoint" ,
} , {
Value : "s3.seo-ap-geo.objectstorage.service.networklayer.com" ,
Help : "APAC Cross Regional Seoul Private Endpoint" ,
} , {
Value : "s3.mel01.objectstorage.softlayer.net" ,
Help : "Melbourne Single Site Endpoint" ,
} , {
Value : "s3.mel01.objectstorage.service.networklayer.com" ,
Help : "Melbourne Single Site Private Endpoint" ,
} , {
Value : "s3.tor01.objectstorage.softlayer.net" ,
Help : "Toronto Single Site Endpoint" ,
} , {
Value : "s3.tor01.objectstorage.service.networklayer.com" ,
Help : "Toronto Single Site Private Endpoint" ,
} } ,
2019-01-12 17:46:45 +01:00
} , {
// oss endpoints: https://help.aliyun.com/document_detail/31837.html
Name : "endpoint" ,
Help : "Endpoint for OSS API." ,
Provider : "Alibaba" ,
Examples : [ ] fs . OptionExample { {
Value : "oss-cn-hangzhou.aliyuncs.com" ,
Help : "East China 1 (Hangzhou)" ,
} , {
Value : "oss-cn-shanghai.aliyuncs.com" ,
Help : "East China 2 (Shanghai)" ,
} , {
Value : "oss-cn-qingdao.aliyuncs.com" ,
Help : "North China 1 (Qingdao)" ,
} , {
Value : "oss-cn-beijing.aliyuncs.com" ,
Help : "North China 2 (Beijing)" ,
} , {
Value : "oss-cn-zhangjiakou.aliyuncs.com" ,
Help : "North China 3 (Zhangjiakou)" ,
} , {
Value : "oss-cn-huhehaote.aliyuncs.com" ,
Help : "North China 5 (Huhehaote)" ,
} , {
Value : "oss-cn-shenzhen.aliyuncs.com" ,
Help : "South China 1 (Shenzhen)" ,
} , {
Value : "oss-cn-hongkong.aliyuncs.com" ,
Help : "Hong Kong (Hong Kong)" ,
} , {
Value : "oss-us-west-1.aliyuncs.com" ,
Help : "US West 1 (Silicon Valley)" ,
} , {
Value : "oss-us-east-1.aliyuncs.com" ,
Help : "US East 1 (Virginia)" ,
} , {
Value : "oss-ap-southeast-1.aliyuncs.com" ,
Help : "Southeast Asia Southeast 1 (Singapore)" ,
} , {
Value : "oss-ap-southeast-2.aliyuncs.com" ,
Help : "Asia Pacific Southeast 2 (Sydney)" ,
} , {
Value : "oss-ap-southeast-3.aliyuncs.com" ,
Help : "Southeast Asia Southeast 3 (Kuala Lumpur)" ,
} , {
Value : "oss-ap-southeast-5.aliyuncs.com" ,
Help : "Asia Pacific Southeast 5 (Jakarta)" ,
} , {
Value : "oss-ap-northeast-1.aliyuncs.com" ,
Help : "Asia Pacific Northeast 1 (Japan)" ,
} , {
Value : "oss-ap-south-1.aliyuncs.com" ,
Help : "Asia Pacific South 1 (Mumbai)" ,
} , {
Value : "oss-eu-central-1.aliyuncs.com" ,
Help : "Central Europe 1 (Frankfurt)" ,
} , {
Value : "oss-eu-west-1.aliyuncs.com" ,
Help : "West Europe (London)" ,
} , {
Value : "oss-me-east-1.aliyuncs.com" ,
Help : "Middle East 1 (Dubai)" ,
} } ,
2018-04-13 17:08:00 +02:00
} , {
Name : "endpoint" ,
Help : "Endpoint for S3 API.\nRequired when using an S3 clone." ,
2019-01-12 17:46:45 +01:00
Provider : "!AWS,IBMCOS,Alibaba" ,
2018-04-13 17:08:00 +02:00
Examples : [ ] fs . OptionExample { {
2019-02-13 22:10:43 +01:00
Value : "objects-us-east-1.dream.io" ,
2018-04-13 17:08:00 +02:00
Help : "Dream Objects endpoint" ,
Provider : "Dreamhost" ,
} , {
Value : "nyc3.digitaloceanspaces.com" ,
Help : "Digital Ocean Spaces New York 3" ,
Provider : "DigitalOcean" ,
} , {
Value : "ams3.digitaloceanspaces.com" ,
Help : "Digital Ocean Spaces Amsterdam 3" ,
Provider : "DigitalOcean" ,
} , {
Value : "sgp1.digitaloceanspaces.com" ,
Help : "Digital Ocean Spaces Singapore 1" ,
Provider : "DigitalOcean" ,
} , {
Value : "s3.wasabisys.com" ,
2018-11-17 17:24:00 +01:00
Help : "Wasabi US East endpoint" ,
Provider : "Wasabi" ,
} , {
Value : "s3.us-west-1.wasabisys.com" ,
Help : "Wasabi US West endpoint" ,
2018-04-13 17:08:00 +02:00
Provider : "Wasabi" ,
2019-05-15 10:22:06 +02:00
} , {
Value : "s3.eu-central-1.wasabisys.com" ,
Help : "Wasabi EU Central endpoint" ,
Provider : "Wasabi" ,
2018-04-13 17:08:00 +02:00
} } ,
} , {
Name : "location_constraint" ,
2018-05-14 19:06:57 +02:00
Help : "Location constraint - must be set to match the Region.\nUsed when creating buckets only." ,
2018-04-13 17:08:00 +02:00
Provider : "AWS" ,
Examples : [ ] fs . OptionExample { {
Value : "" ,
Help : "Empty for US Region, Northern Virginia or Pacific Northwest." ,
} , {
Value : "us-east-2" ,
Help : "US East (Ohio) Region." ,
} , {
Value : "us-west-2" ,
Help : "US West (Oregon) Region." ,
} , {
Value : "us-west-1" ,
Help : "US West (Northern California) Region." ,
} , {
Value : "ca-central-1" ,
Help : "Canada (Central) Region." ,
} , {
Value : "eu-west-1" ,
Help : "EU (Ireland) Region." ,
} , {
Value : "eu-west-2" ,
Help : "EU (London) Region." ,
2019-02-02 20:44:26 +01:00
} , {
Value : "eu-north-1" ,
Help : "EU (Stockholm) Region." ,
2018-04-13 17:08:00 +02:00
} , {
Value : "EU" ,
Help : "EU Region." ,
} , {
Value : "ap-southeast-1" ,
Help : "Asia Pacific (Singapore) Region." ,
} , {
Value : "ap-southeast-2" ,
Help : "Asia Pacific (Sydney) Region." ,
} , {
Value : "ap-northeast-1" ,
Help : "Asia Pacific (Tokyo) Region." ,
} , {
Value : "ap-northeast-2" ,
Help : "Asia Pacific (Seoul)" ,
} , {
Value : "ap-south-1" ,
Help : "Asia Pacific (Mumbai)" ,
2020-01-02 12:10:48 +01:00
} , {
Value : "ap-east-1" ,
Help : "Asia Pacific (Hong Kong)" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "sa-east-1" ,
Help : "South America (Sao Paulo) Region." ,
} } ,
} , {
Name : "location_constraint" ,
2018-05-14 19:06:57 +02:00
Help : "Location constraint - must match endpoint when using IBM Cloud Public.\nFor on-prem COS, do not make a selection from this list, hit enter" ,
2018-04-13 17:08:00 +02:00
Provider : "IBMCOS" ,
Examples : [ ] fs . OptionExample { {
Value : "us-standard" ,
Help : "US Cross Region Standard" ,
} , {
Value : "us-vault" ,
Help : "US Cross Region Vault" ,
} , {
Value : "us-cold" ,
Help : "US Cross Region Cold" ,
} , {
Value : "us-flex" ,
Help : "US Cross Region Flex" ,
} , {
Value : "us-east-standard" ,
Help : "US East Region Standard" ,
} , {
Value : "us-east-vault" ,
Help : "US East Region Vault" ,
} , {
Value : "us-east-cold" ,
Help : "US East Region Cold" ,
} , {
Value : "us-east-flex" ,
Help : "US East Region Flex" ,
} , {
Value : "us-south-standard" ,
2019-02-07 18:41:17 +01:00
Help : "US South Region Standard" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "us-south-vault" ,
Help : "US South Region Vault" ,
} , {
Value : "us-south-cold" ,
Help : "US South Region Cold" ,
} , {
Value : "us-south-flex" ,
Help : "US South Region Flex" ,
} , {
Value : "eu-standard" ,
Help : "EU Cross Region Standard" ,
} , {
Value : "eu-vault" ,
Help : "EU Cross Region Vault" ,
} , {
Value : "eu-cold" ,
Help : "EU Cross Region Cold" ,
} , {
Value : "eu-flex" ,
Help : "EU Cross Region Flex" ,
} , {
Value : "eu-gb-standard" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Standard" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "eu-gb-vault" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Vault" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "eu-gb-cold" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Cold" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "eu-gb-flex" ,
2019-02-07 18:41:17 +01:00
Help : "Great Britain Flex" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "ap-standard" ,
Help : "APAC Standard" ,
} , {
Value : "ap-vault" ,
Help : "APAC Vault" ,
} , {
Value : "ap-cold" ,
Help : "APAC Cold" ,
} , {
Value : "ap-flex" ,
Help : "APAC Flex" ,
} , {
Value : "mel01-standard" ,
Help : "Melbourne Standard" ,
} , {
Value : "mel01-vault" ,
Help : "Melbourne Vault" ,
} , {
Value : "mel01-cold" ,
Help : "Melbourne Cold" ,
} , {
Value : "mel01-flex" ,
Help : "Melbourne Flex" ,
} , {
Value : "tor01-standard" ,
Help : "Toronto Standard" ,
} , {
Value : "tor01-vault" ,
Help : "Toronto Vault" ,
} , {
Value : "tor01-cold" ,
Help : "Toronto Cold" ,
} , {
Value : "tor01-flex" ,
Help : "Toronto Flex" ,
} } ,
} , {
Name : "location_constraint" ,
2018-05-14 19:06:57 +02:00
Help : "Location constraint - must be set to match the Region.\nLeave blank if not sure. Used when creating buckets only." ,
2019-01-12 17:46:45 +01:00
Provider : "!AWS,IBMCOS,Alibaba" ,
2018-04-13 17:08:00 +02:00
} , {
Name : "acl" ,
2018-10-25 23:19:36 +02:00
Help : ` Canned ACL used when creating buckets and storing or copying objects .
2019-01-16 18:23:37 +01:00
This ACL is used for creating objects and if bucket_acl isn ' t set , for creating buckets too .
2018-10-25 23:19:36 +02:00
For more info visit https : //docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
Note that this ACL is applied when server side copying objects as S3
doesn ' t copy the ACL from the source but rather writes a fresh one . ` ,
2018-04-13 17:08:00 +02:00
Examples : [ ] fs . OptionExample { {
Value : "private" ,
Help : "Owner gets FULL_CONTROL. No one else has access rights (default)." ,
Provider : "!IBMCOS" ,
} , {
Value : "public-read" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ access." ,
Provider : "!IBMCOS" ,
} , {
Value : "public-read-write" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ and WRITE access.\nGranting this on a bucket is generally not recommended." ,
Provider : "!IBMCOS" ,
} , {
Value : "authenticated-read" ,
Help : "Owner gets FULL_CONTROL. The AuthenticatedUsers group gets READ access." ,
Provider : "!IBMCOS" ,
} , {
Value : "bucket-owner-read" ,
Help : "Object owner gets FULL_CONTROL. Bucket owner gets READ access.\nIf you specify this canned ACL when creating a bucket, Amazon S3 ignores it." ,
Provider : "!IBMCOS" ,
} , {
Value : "bucket-owner-full-control" ,
Help : "Both the object owner and the bucket owner get FULL_CONTROL over the object.\nIf you specify this canned ACL when creating a bucket, Amazon S3 ignores it." ,
Provider : "!IBMCOS" ,
} , {
Value : "private" ,
Help : "Owner gets FULL_CONTROL. No one else has access rights (default). This acl is available on IBM Cloud (Infra), IBM Cloud (Storage), On-Premise COS" ,
2018-04-12 18:05:53 +02:00
Provider : "IBMCOS" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "public-read" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ access. This acl is available on IBM Cloud (Infra), IBM Cloud (Storage), On-Premise IBM COS" ,
Provider : "IBMCOS" ,
} , {
Value : "public-read-write" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ and WRITE access. This acl is available on IBM Cloud (Infra), On-Premise IBM COS" ,
2018-04-12 18:05:53 +02:00
Provider : "IBMCOS" ,
2018-04-13 17:08:00 +02:00
} , {
Value : "authenticated-read" ,
Help : "Owner gets FULL_CONTROL. The AuthenticatedUsers group gets READ access. Not supported on Buckets. This acl is available on IBM Cloud (Infra) and On-Premise IBM COS" ,
Provider : "IBMCOS" ,
} } ,
2019-01-16 18:23:37 +01:00
} , {
Name : "bucket_acl" ,
Help : ` Canned ACL used when creating buckets .
For more info visit https : //docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
Note that this ACL is applied when only when creating buckets . If it
isn ' t set then "acl" is used instead . ` ,
Advanced : true ,
Examples : [ ] fs . OptionExample { {
Value : "private" ,
Help : "Owner gets FULL_CONTROL. No one else has access rights (default)." ,
} , {
Value : "public-read" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ access." ,
} , {
Value : "public-read-write" ,
Help : "Owner gets FULL_CONTROL. The AllUsers group gets READ and WRITE access.\nGranting this on a bucket is generally not recommended." ,
} , {
Value : "authenticated-read" ,
Help : "Owner gets FULL_CONTROL. The AuthenticatedUsers group gets READ access." ,
} } ,
2018-04-13 17:08:00 +02:00
} , {
Name : "server_side_encryption" ,
Help : "The server-side encryption algorithm used when storing this object in S3." ,
Provider : "AWS" ,
Examples : [ ] fs . OptionExample { {
Value : "" ,
Help : "None" ,
} , {
Value : "AES256" ,
Help : "AES256" ,
2018-08-30 18:08:27 +02:00
} , {
Value : "aws:kms" ,
Help : "aws:kms" ,
} } ,
} , {
Name : "sse_kms_key_id" ,
Help : "If using KMS ID you must provide the ARN of Key." ,
Provider : "AWS" ,
Examples : [ ] fs . OptionExample { {
Value : "" ,
Help : "None" ,
} , {
Value : "arn:aws:kms:us-east-1:*" ,
Help : "arn:aws:kms:*" ,
2018-04-13 17:08:00 +02:00
} } ,
} , {
Name : "storage_class" ,
2018-10-01 19:36:15 +02:00
Help : "The storage class to use when storing new objects in S3." ,
2018-04-13 17:08:00 +02:00
Provider : "AWS" ,
Examples : [ ] fs . OptionExample { {
Value : "" ,
Help : "Default" ,
} , {
Value : "STANDARD" ,
Help : "Standard storage class" ,
} , {
Value : "REDUCED_REDUNDANCY" ,
Help : "Reduced redundancy storage class" ,
} , {
Value : "STANDARD_IA" ,
Help : "Standard Infrequent Access storage class" ,
} , {
Value : "ONEZONE_IA" ,
Help : "One Zone Infrequent Access storage class" ,
2018-12-06 22:53:05 +01:00
} , {
Value : "GLACIER" ,
Help : "Glacier storage class" ,
2019-04-05 05:14:05 +02:00
} , {
Value : "DEEP_ARCHIVE" ,
Help : "Glacier Deep Archive storage class" ,
2019-04-04 19:04:28 +02:00
} , {
Value : "INTELLIGENT_TIERING" ,
Help : "Intelligent-Tiering storage class" ,
2018-04-13 17:08:00 +02:00
} } ,
2019-01-12 17:46:45 +01:00
} , {
2019-01-12 21:41:47 +01:00
// Mapping from here: https://www.alibabacloud.com/help/doc-detail/64919.htm
2019-01-12 17:46:45 +01:00
Name : "storage_class" ,
Help : "The storage class to use when storing new objects in OSS." ,
Provider : "Alibaba" ,
Examples : [ ] fs . OptionExample { {
2019-01-12 21:41:47 +01:00
Value : "" ,
Help : "Default" ,
} , {
Value : "STANDARD" ,
2019-01-12 17:46:45 +01:00
Help : "Standard storage class" ,
} , {
2019-01-12 21:41:47 +01:00
Value : "GLACIER" ,
2019-01-12 17:46:45 +01:00
Help : "Archive storage mode." ,
} , {
2019-01-12 21:41:47 +01:00
Value : "STANDARD_IA" ,
2019-01-12 17:46:45 +01:00
Help : "Infrequent access storage mode." ,
} } ,
2018-11-26 22:09:23 +01:00
} , {
Name : "upload_cutoff" ,
Help : ` Cutoff for switching to chunked upload
Any files larger than this will be uploaded in chunks of chunk_size .
The minimum is 0 and the maximum is 5 GB . ` ,
Default : defaultUploadCutoff ,
Advanced : true ,
2018-05-14 19:06:57 +02:00
} , {
2018-10-01 19:36:15 +02:00
Name : "chunk_size" ,
Help : ` Chunk size to use for uploading .
2019-11-06 11:41:03 +01:00
When uploading files larger than upload_cutoff or files with unknown
size ( eg from "rclone rcat" or uploaded with "rclone mount" or google
photos or google docs ) they will be uploaded as multipart uploads
using this chunk size .
2018-10-01 19:36:15 +02:00
Note that "--s3-upload-concurrency" chunks of this size are buffered
in memory per transfer .
If you are transferring large files over high speed links and you have
2019-11-06 11:41:03 +01:00
enough memory , then increasing this will speed up the transfers .
Rclone will automatically increase the chunk size when uploading a
large file of known size to stay below the 10 , 000 chunks limit .
Files of unknown size are uploaded with the configured
chunk_size . Since the default chunk size is 5 MB and there can be at
most 10 , 000 chunks , this means that by default the maximum size of
file you can stream upload is 48 GB . If you wish to stream upload
larger files then you will need to increase chunk_size . ` ,
2018-09-07 13:02:27 +02:00
Default : minChunkSize ,
2018-05-14 19:06:57 +02:00
Advanced : true ,
2019-12-02 18:14:57 +01:00
} , {
Name : "copy_cutoff" ,
Help : ` Cutoff for switching to multipart copy
Any files larger than this that need to be server side copied will be
copied in chunks of this size .
The minimum is 0 and the maximum is 5 GB . ` ,
Default : fs . SizeSuffix ( maxSizeForCopy ) ,
Advanced : true ,
2018-05-14 19:06:57 +02:00
} , {
Name : "disable_checksum" ,
Help : "Don't store MD5 checksum with object metadata" ,
Default : false ,
Advanced : true ,
} , {
Name : "session_token" ,
Help : "An AWS session token" ,
Advanced : true ,
} , {
2018-10-01 19:36:15 +02:00
Name : "upload_concurrency" ,
Help : ` Concurrency for multipart uploads .
This is the number of chunks of the same file that are uploaded
concurrently .
If you are uploading small numbers of large file over high speed link
and these uploads do not fully utilize your bandwidth , then increasing
this may help to speed up the transfers . ` ,
2018-12-02 18:51:14 +01:00
Default : 4 ,
2018-05-14 19:06:57 +02:00
Advanced : true ,
2018-07-18 17:40:59 +02:00
} , {
2018-10-01 19:36:15 +02:00
Name : "force_path_style" ,
Help : ` If true use path style access if false use virtual hosted style .
If this is true ( the default ) then rclone will use path style access ,
if false then rclone will use virtual path style . See [ the AWS S3
docs ] ( https : //docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro)
for more info .
Some providers ( eg Aliyun OSS or Netease COS ) require this set to false . ` ,
2018-07-18 17:40:59 +02:00
Default : true ,
Advanced : true ,
2018-10-09 14:03:37 +02:00
} , {
Name : "v2_auth" ,
Help : ` If true use v2 authentication .
If this is false ( the default ) then rclone will use v4 authentication .
If it is set then rclone will use v2 authentication .
Use this only if v4 signatures don ' t work , eg pre Jewel / v10 CEPH . ` ,
Default : false ,
Advanced : true ,
2019-04-26 11:19:00 +02:00
} , {
Name : "use_accelerate_endpoint" ,
Provider : "AWS" ,
Help : ` If true use the AWS S3 accelerated endpoint .
See : [ AWS S3 Transfer acceleration ] ( https : //docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration-examples.html)`,
Default : false ,
Advanced : true ,
2019-07-24 11:03:38 +02:00
} , {
Name : "leave_parts_on_error" ,
Provider : "AWS" ,
Help : ` If true avoid calling abort upload on a failure , leaving all successfully uploaded parts on S3 for manual recovery .
It should be set to true for resuming uploads across different sessions .
WARNING : Storing parts of an incomplete multipart upload counts towards space usage on S3 and will add additional costs if not cleaned up .
` ,
Default : false ,
Advanced : true ,
2019-12-26 12:05:00 +01:00
} , {
Name : "list_chunk" ,
Help : ` Size of listing chunk ( response list for each ListObject S3 request ) .
This option is also known as "MaxKeys" , "max-items" , or "page-size" from the AWS S3 specification .
Most services truncate the response list to 1000 objects even if requested more than that .
In AWS S3 this is a global maximum and cannot be changed , see [ AWS S3 ] ( https : //docs.aws.amazon.com/cli/latest/reference/s3/ls.html).
In Ceph , this can be increased with the "rgw list buckets max chunk" option .
` ,
Default : 1000 ,
Advanced : true ,
2018-05-14 19:06:57 +02:00
} } ,
2014-03-15 17:06:11 +01:00
} )
2013-06-27 21:13:07 +02:00
}
2013-01-08 19:53:35 +01:00
// Constants
const (
2019-11-06 11:41:03 +01:00
metaMtime = "Mtime" // the meta key to store mtime in - eg X-Amz-Meta-Mtime
metaMD5Hash = "Md5chksum" // the meta key to store md5hash in
maxRetries = 10 // number of retries to make of operations
maxSizeForCopy = 5 * 1024 * 1024 * 1024 // The maximum size of object we can COPY
2018-11-26 22:09:23 +01:00
minChunkSize = fs . SizeSuffix ( s3manager . MinUploadPartSize )
defaultUploadCutoff = fs . SizeSuffix ( 200 * 1024 * 1024 )
maxUploadCutoff = fs . SizeSuffix ( 5 * 1024 * 1024 * 1024 )
minSleep = 10 * time . Millisecond // In case of error, start at 10ms sleep.
2013-01-08 19:53:35 +01:00
)
2018-05-14 19:06:57 +02:00
// Options defines the configuration for this backend
type Options struct {
2019-04-26 11:19:00 +02:00
Provider string ` config:"provider" `
EnvAuth bool ` config:"env_auth" `
AccessKeyID string ` config:"access_key_id" `
SecretAccessKey string ` config:"secret_access_key" `
Region string ` config:"region" `
Endpoint string ` config:"endpoint" `
LocationConstraint string ` config:"location_constraint" `
ACL string ` config:"acl" `
BucketACL string ` config:"bucket_acl" `
ServerSideEncryption string ` config:"server_side_encryption" `
SSEKMSKeyID string ` config:"sse_kms_key_id" `
StorageClass string ` config:"storage_class" `
UploadCutoff fs . SizeSuffix ` config:"upload_cutoff" `
2019-12-02 18:14:57 +01:00
CopyCutoff fs . SizeSuffix ` config:"copy_cutoff" `
2019-04-26 11:19:00 +02:00
ChunkSize fs . SizeSuffix ` config:"chunk_size" `
DisableChecksum bool ` config:"disable_checksum" `
SessionToken string ` config:"session_token" `
UploadConcurrency int ` config:"upload_concurrency" `
ForcePathStyle bool ` config:"force_path_style" `
V2Auth bool ` config:"v2_auth" `
UseAccelerateEndpoint bool ` config:"use_accelerate_endpoint" `
2019-07-24 11:03:38 +02:00
LeavePartsOnError bool ` config:"leave_parts_on_error" `
2019-12-26 12:05:00 +01:00
ListChunk int64 ` config:"list_chunk" `
2018-05-14 19:06:57 +02:00
}
2016-09-01 23:27:50 +02:00
2015-11-07 12:14:46 +01:00
// Fs represents a remote s3 server
type Fs struct {
2018-05-14 19:06:57 +02:00
name string // the name of the remote
root string // root of the bucket - ignore all objects above this
opt Options // parsed options
features * fs . Features // optional features
c * s3 . S3 // the connection to the s3 server
ses * session . Session // the s3 session
2019-08-09 12:29:36 +02:00
rootBucket string // bucket part of root (if any)
rootDirectory string // directory part of root (if any)
cache * bucket . Cache // cache for bucket creation status
2019-02-09 21:52:15 +01:00
pacer * fs . Pacer // To pace the API calls
2018-11-26 22:09:23 +01:00
srv * http . Client // a plain http client
2013-01-08 19:53:35 +01:00
}
2015-11-07 12:14:46 +01:00
// Object describes a s3 object
type Object struct {
2013-01-08 19:53:35 +01:00
// Will definitely have everything but meta which may be nil
//
2016-09-21 23:13:24 +02:00
// List will read everything but meta & mimeType - to fill
// that in you need to call readMetaData
2015-11-07 12:14:46 +01:00
fs * Fs // what this object is part of
2014-12-23 13:09:02 +01:00
remote string // The remote path
etag string // md5sum of the object
bytes int64 // size of the object
lastModified time . Time // Last modified
meta map [ string ] * string // The object metadata if known - may be nil
2016-09-21 23:13:24 +02:00
mimeType string // MimeType of object - may be ""
2019-09-09 21:44:50 +02:00
storageClass string // eg GLACIER
2013-01-08 19:53:35 +01:00
}
// ------------------------------------------------------------
2015-09-22 19:47:16 +02:00
// Name of the remote (as passed into NewFs)
2015-11-07 12:14:46 +01:00
func ( f * Fs ) Name ( ) string {
2015-08-22 17:53:11 +02:00
return f . name
}
2015-09-22 19:47:16 +02:00
// Root of the remote (as passed into NewFs)
2015-11-07 12:14:46 +01:00
func ( f * Fs ) Root ( ) string {
2019-08-09 12:29:36 +02:00
return f . root
2015-09-01 21:45:27 +02:00
}
2015-11-07 12:14:46 +01:00
// String converts this Fs to a string
func ( f * Fs ) String ( ) string {
2019-08-09 12:29:36 +02:00
if f . rootBucket == "" {
return fmt . Sprintf ( "S3 root" )
}
if f . rootDirectory == "" {
return fmt . Sprintf ( "S3 bucket %s" , f . rootBucket )
2014-05-05 19:25:32 +02:00
}
2019-08-09 12:29:36 +02:00
return fmt . Sprintf ( "S3 bucket %s path %s" , f . rootBucket , f . rootDirectory )
2013-01-08 19:53:35 +01:00
}
2017-01-13 18:21:47 +01:00
// Features returns the optional features of this Fs
func ( f * Fs ) Features ( ) * fs . Features {
return f . features
}
2018-09-03 06:41:04 +02:00
// retryErrorCodes is a slice of error codes that we will retry
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
var retryErrorCodes = [ ] int {
2019-01-12 17:46:45 +01:00
// 409, // Conflict - various states that could be resolved on a retry
2018-09-03 06:41:04 +02:00
503 , // Service Unavailable/Slow Down - "Reduce your request rate"
}
//S3 is pretty resilient, and the built in retry handling is probably sufficient
// as it should notice closed connections and timeouts which are the most likely
// sort of failure modes
2019-01-16 14:35:19 +01:00
func ( f * Fs ) shouldRetry ( err error ) ( bool , error ) {
2018-09-03 06:41:04 +02:00
// If this is an awserr object, try and extract more useful information to determine if we should retry
if awsError , ok := err . ( awserr . Error ) ; ok {
2019-02-07 18:41:17 +01:00
// Simple case, check the original embedded error in case it's generically retryable
2018-09-03 06:41:04 +02:00
if fserrors . ShouldRetry ( awsError . OrigErr ( ) ) {
return true , err
}
2019-01-12 17:46:45 +01:00
// Failing that, if it's a RequestFailure it's probably got an http status code we can check
2018-09-03 06:41:04 +02:00
if reqErr , ok := err . ( awserr . RequestFailure ) ; ok {
2019-08-09 12:29:36 +02:00
// 301 if wrong region for bucket - can only update if running from a bucket
if f . rootBucket != "" {
if reqErr . StatusCode ( ) == http . StatusMovedPermanently {
urfbErr := f . updateRegionForBucket ( f . rootBucket )
if urfbErr != nil {
fs . Errorf ( f , "Failed to update region for bucket: %v" , urfbErr )
return false , err
}
return true , err
2019-01-16 14:35:19 +01:00
}
}
2018-09-03 06:41:04 +02:00
for _ , e := range retryErrorCodes {
if reqErr . StatusCode ( ) == e {
return true , err
}
}
}
}
2019-01-12 17:46:45 +01:00
// Ok, not an awserr, check for generic failure conditions
2018-09-03 06:41:04 +02:00
return fserrors . ShouldRetry ( err ) , err
}
2019-08-09 12:29:36 +02:00
// parsePath parses a remote 'url'
func parsePath ( path string ) ( root string ) {
root = strings . Trim ( path , "/" )
2013-01-08 19:53:35 +01:00
return
}
2019-08-09 12:29:36 +02:00
// split returns bucket and bucketPath from the rootRelativePath
// relative to f.root
func ( f * Fs ) split ( rootRelativePath string ) ( bucketName , bucketPath string ) {
2018-11-02 13:15:30 +01:00
bucketName , bucketPath = bucket . Split ( path . Join ( f . root , rootRelativePath ) )
return enc . FromStandardName ( bucketName ) , enc . FromStandardPath ( bucketPath )
2019-08-09 12:29:36 +02:00
}
// split returns bucket and bucketPath from the object
func ( o * Object ) split ( ) ( bucket , bucketPath string ) {
return o . fs . split ( o . remote )
}
2013-01-08 19:53:35 +01:00
// s3Connection makes a connection to s3
2018-05-14 19:06:57 +02:00
func s3Connection ( opt * Options ) ( * s3 . S3 , * session . Session , error ) {
2013-01-08 19:53:35 +01:00
// Make the auth
2016-02-01 14:11:27 +01:00
v := credentials . Value {
2018-05-14 19:06:57 +02:00
AccessKeyID : opt . AccessKeyID ,
SecretAccessKey : opt . SecretAccessKey ,
SessionToken : opt . SessionToken ,
2016-02-01 14:11:27 +01:00
}
2017-11-22 22:21:36 +01:00
lowTimeoutClient := & http . Client { Timeout : 1 * time . Second } // low timeout to ec2 metadata service
def := defaults . Get ( )
def . Config . HTTPClient = lowTimeoutClient
2016-02-01 14:11:27 +01:00
// first provider to supply a credential set "wins"
providers := [ ] credentials . Provider {
// use static credentials if they're present (checked by provider)
& credentials . StaticProvider { Value : v } ,
// * Access Key ID: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY
// * Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY
& credentials . EnvProvider { } ,
2018-04-16 13:14:35 +02:00
// A SharedCredentialsProvider retrieves credentials
// from the current user's home directory. It checks
// AWS_SHARED_CREDENTIALS_FILE and AWS_PROFILE too.
& credentials . SharedCredentialsProvider { } ,
2017-11-22 22:21:36 +01:00
// Pick up IAM role if we're in an ECS task
defaults . RemoteCredProvider ( * def . Config , def . Handlers ) ,
2016-02-01 14:11:27 +01:00
// Pick up IAM role in case we're on EC2
& ec2rolecreds . EC2RoleProvider {
Client : ec2metadata . New ( session . New ( ) , & aws . Config {
2017-11-22 22:21:36 +01:00
HTTPClient : lowTimeoutClient ,
2016-02-01 14:11:27 +01:00
} ) ,
2019-10-31 16:42:16 +01:00
ExpiryWindow : 3 * time . Minute ,
2016-02-01 14:11:27 +01:00
} ,
}
cred := credentials . NewChainCredentials ( providers )
2015-09-29 10:58:03 +02:00
switch {
2018-05-14 19:06:57 +02:00
case opt . EnvAuth :
2016-02-09 18:19:33 +01:00
// No need for empty checks if "env_auth" is true
case v . AccessKeyID == "" && v . SecretAccessKey == "" :
2016-02-01 14:11:27 +01:00
// if no access key/secret and iam is explicitly disabled then fall back to anon interaction
cred = credentials . AnonymousCredentials
case v . AccessKeyID == "" :
2015-10-30 12:50:45 +01:00
return nil , nil , errors . New ( "access_key_id not found" )
2016-02-01 14:11:27 +01:00
case v . SecretAccessKey == "" :
2015-10-30 12:50:45 +01:00
return nil , nil , errors . New ( "secret_access_key not found" )
2013-01-08 19:53:35 +01:00
}
2018-05-14 19:06:57 +02:00
if opt . Region == "" && opt . Endpoint == "" {
opt . Endpoint = "https://s3.amazonaws.com/"
2013-01-08 19:53:35 +01:00
}
2018-05-14 19:06:57 +02:00
if opt . Region == "" {
opt . Region = "us-east-1"
2013-01-08 19:53:35 +01:00
}
2019-04-26 11:19:00 +02:00
if opt . Provider == "Alibaba" || opt . Provider == "Netease" || opt . UseAccelerateEndpoint {
2019-01-12 17:46:45 +01:00
opt . ForcePathStyle = false
}
2014-12-23 13:09:02 +01:00
awsConfig := aws . NewConfig ( ) .
WithMaxRetries ( maxRetries ) .
2016-02-01 14:11:27 +01:00
WithCredentials ( cred ) .
2018-01-12 17:30:54 +01:00
WithHTTPClient ( fshttp . NewClient ( fs . Config ) ) .
2019-04-26 11:19:00 +02:00
WithS3ForcePathStyle ( opt . ForcePathStyle ) .
WithS3UseAccelerate ( opt . UseAccelerateEndpoint )
2019-01-16 14:35:19 +01:00
if opt . Region != "" {
awsConfig . WithRegion ( opt . Region )
}
if opt . Endpoint != "" {
awsConfig . WithEndpoint ( opt . Endpoint )
}
2015-08-10 12:02:34 +02:00
// awsConfig.WithLogLevel(aws.LogDebugWithSigning)
2018-11-07 03:50:28 +01:00
awsSessionOpts := session . Options {
Config : * awsConfig ,
}
if opt . EnvAuth && opt . AccessKeyID == "" && opt . SecretAccessKey == "" {
// Enable loading config options from ~/.aws/config (selected by AWS_PROFILE env)
awsSessionOpts . SharedConfigState = session . SharedConfigEnable
// The session constructor (aws/session/mergeConfigSrcs) will only use the user's preferred credential source
// (from the shared config file) if the passed-in Options.Config.Credentials is nil.
awsSessionOpts . Config . Credentials = nil
}
ses , err := session . NewSessionWithOptions ( awsSessionOpts )
if err != nil {
return nil , nil , err
}
c := s3 . New ( ses )
2018-10-09 14:03:37 +02:00
if opt . V2Auth || opt . Region == "other-v2-signature" {
2018-05-14 19:06:57 +02:00
fs . Debugf ( nil , "Using v2 auth" )
2015-08-28 09:47:41 +02:00
signer := func ( req * request . Request ) {
2015-08-10 12:02:34 +02:00
// Ignore AnonymousCredentials object
2015-10-30 12:50:45 +01:00
if req . Config . Credentials == credentials . AnonymousCredentials {
2015-08-10 12:02:34 +02:00
return
}
2016-02-01 14:11:27 +01:00
sign ( v . AccessKeyID , v . SecretAccessKey , req . HTTPRequest )
2015-08-10 12:02:34 +02:00
}
c . Handlers . Sign . Clear ( )
2015-08-28 09:47:41 +02:00
c . Handlers . Sign . PushBackNamed ( corehandlers . BuildContentLengthHandler )
2015-08-10 12:02:34 +02:00
c . Handlers . Sign . PushBack ( signer )
2013-01-08 19:53:35 +01:00
}
2015-10-30 12:50:45 +01:00
return c , ses , nil
2013-01-08 19:53:35 +01:00
}
2018-09-07 13:02:27 +02:00
func checkUploadChunkSize ( cs fs . SizeSuffix ) error {
if cs < minChunkSize {
return errors . Errorf ( "%s is less than %s" , cs , minChunkSize )
}
return nil
}
func ( f * Fs ) setUploadChunkSize ( cs fs . SizeSuffix ) ( old fs . SizeSuffix , err error ) {
err = checkUploadChunkSize ( cs )
if err == nil {
old , f . opt . ChunkSize = f . opt . ChunkSize , cs
}
return
}
2018-11-26 22:09:23 +01:00
func checkUploadCutoff ( cs fs . SizeSuffix ) error {
if cs > maxUploadCutoff {
return errors . Errorf ( "%s is greater than %s" , cs , maxUploadCutoff )
}
return nil
}
func ( f * Fs ) setUploadCutoff ( cs fs . SizeSuffix ) ( old fs . SizeSuffix , err error ) {
err = checkUploadCutoff ( cs )
if err == nil {
old , f . opt . UploadCutoff = f . opt . UploadCutoff , cs
}
return
}
2019-08-09 12:29:36 +02:00
// setRoot changes the root of the Fs
func ( f * Fs ) setRoot ( root string ) {
f . root = parsePath ( root )
f . rootBucket , f . rootDirectory = bucket . Split ( f . root )
}
2016-09-01 23:27:50 +02:00
// NewFs constructs an Fs from the path, bucket:path
2018-05-14 19:06:57 +02:00
func NewFs ( name , root string , m configmap . Mapper ) ( fs . Fs , error ) {
// Parse config into Options struct
opt := new ( Options )
err := configstruct . Set ( m , opt )
if err != nil {
return nil , err
}
2018-09-07 13:02:27 +02:00
err = checkUploadChunkSize ( opt . ChunkSize )
if err != nil {
return nil , errors . Wrap ( err , "s3: chunk size" )
2018-05-14 19:06:57 +02:00
}
2018-11-26 22:09:23 +01:00
err = checkUploadCutoff ( opt . UploadCutoff )
if err != nil {
return nil , errors . Wrap ( err , "s3: upload cutoff" )
}
2019-01-16 18:23:37 +01:00
if opt . ACL == "" {
opt . ACL = "private"
}
if opt . BucketACL == "" {
opt . BucketACL = opt . ACL
}
2018-05-14 19:06:57 +02:00
c , ses , err := s3Connection ( opt )
2013-01-08 19:53:35 +01:00
if err != nil {
return nil , err
}
2015-11-07 12:14:46 +01:00
f := & Fs {
2019-08-09 12:29:36 +02:00
name : name ,
opt : * opt ,
c : c ,
ses : ses ,
pacer : fs . NewPacer ( pacer . NewS3 ( pacer . MinSleep ( minSleep ) ) ) ,
cache : bucket . NewCache ( ) ,
srv : fshttp . NewClient ( fs . Config ) ,
}
f . setRoot ( root )
2017-08-09 16:27:43 +02:00
f . features = ( & fs . Features {
2019-08-09 12:29:36 +02:00
ReadMimeType : true ,
WriteMimeType : true ,
BucketBased : true ,
BucketBasedRootOK : true ,
2019-09-09 21:44:50 +02:00
SetTier : true ,
GetTier : true ,
2017-08-09 16:27:43 +02:00
} ) . Fill ( f )
2019-08-09 12:29:36 +02:00
if f . rootBucket != "" && f . rootDirectory != "" {
2014-05-05 20:52:52 +02:00
// Check to see if the object exists
2018-11-02 13:15:30 +01:00
encodedDirectory := enc . FromStandardPath ( f . rootDirectory )
2014-12-23 13:09:02 +01:00
req := s3 . HeadObjectInput {
2019-08-09 12:29:36 +02:00
Bucket : & f . rootBucket ,
2018-11-02 13:15:30 +01:00
Key : & encodedDirectory ,
2014-12-23 13:09:02 +01:00
}
2018-09-03 06:41:04 +02:00
err = f . pacer . Call ( func ( ) ( bool , error ) {
_ , err = f . c . HeadObject ( & req )
2019-01-16 14:35:19 +01:00
return f . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2014-05-05 20:52:52 +02:00
if err == nil {
2019-08-09 12:29:36 +02:00
newRoot := path . Dir ( f . root )
if newRoot == "." {
newRoot = ""
2014-05-05 20:52:52 +02:00
}
2019-08-09 12:29:36 +02:00
f . setRoot ( newRoot )
2016-06-21 19:01:53 +02:00
// return an error with an fs which points to the parent
return f , fs . ErrorIsFile
2014-05-05 20:52:52 +02:00
}
}
2014-12-23 13:09:02 +01:00
// f.listMultipartUploads()
2013-01-08 19:53:35 +01:00
return f , nil
}
2016-06-25 22:58:34 +02:00
// Return an Object from a path
2013-01-08 19:53:35 +01:00
//
2016-06-25 22:23:20 +02:00
//If it can't be found it returns the error ErrorObjectNotFound.
2019-06-17 10:34:30 +02:00
func ( f * Fs ) newObjectWithInfo ( ctx context . Context , remote string , info * s3 . Object ) ( fs . Object , error ) {
2015-11-07 12:14:46 +01:00
o := & Object {
fs : f ,
2013-01-08 19:53:35 +01:00
remote : remote ,
}
if info != nil {
// Set info but not meta
2014-12-23 13:09:02 +01:00
if info . LastModified == nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Failed to read last modified" )
2013-06-27 21:13:07 +02:00
o . lastModified = time . Now ( )
2014-12-23 13:09:02 +01:00
} else {
o . lastModified = * info . LastModified
2013-01-08 19:53:35 +01:00
}
2014-12-23 13:09:02 +01:00
o . etag = aws . StringValue ( info . ETag )
o . bytes = aws . Int64Value ( info . Size )
2019-09-09 21:44:50 +02:00
o . storageClass = aws . StringValue ( info . StorageClass )
2013-01-08 19:53:35 +01:00
} else {
2019-06-17 10:34:30 +02:00
err := o . readMetaData ( ctx ) // reads info and meta, returning an error
2013-01-08 19:53:35 +01:00
if err != nil {
2016-06-25 22:23:20 +02:00
return nil , err
2013-01-08 19:53:35 +01:00
}
}
2016-06-25 22:23:20 +02:00
return o , nil
2013-01-08 19:53:35 +01:00
}
2016-06-25 22:23:20 +02:00
// NewObject finds the Object at remote. If it can't be found
// it returns the error fs.ErrorObjectNotFound.
2019-06-17 10:34:30 +02:00
func ( f * Fs ) NewObject ( ctx context . Context , remote string ) ( fs . Object , error ) {
return f . newObjectWithInfo ( ctx , remote , nil )
2013-01-08 19:53:35 +01:00
}
2019-01-16 14:35:19 +01:00
// Gets the bucket location
2019-08-09 12:29:36 +02:00
func ( f * Fs ) getBucketLocation ( bucket string ) ( string , error ) {
2019-01-16 14:35:19 +01:00
req := s3 . GetBucketLocationInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
2019-01-16 14:35:19 +01:00
}
var resp * s3 . GetBucketLocationOutput
var err error
err = f . pacer . Call ( func ( ) ( bool , error ) {
resp , err = f . c . GetBucketLocation ( & req )
return f . shouldRetry ( err )
} )
if err != nil {
return "" , err
}
return s3 . NormalizeBucketLocation ( aws . StringValue ( resp . LocationConstraint ) ) , nil
}
// Updates the region for the bucket by reading the region from the
// bucket then updating the session.
2019-08-09 12:29:36 +02:00
func ( f * Fs ) updateRegionForBucket ( bucket string ) error {
region , err := f . getBucketLocation ( bucket )
2019-01-16 14:35:19 +01:00
if err != nil {
return errors . Wrap ( err , "reading bucket location failed" )
}
if aws . StringValue ( f . c . Config . Endpoint ) != "" {
return errors . Errorf ( "can't set region to %q as endpoint is set" , region )
}
if aws . StringValue ( f . c . Config . Region ) == region {
return errors . Errorf ( "region is already %q - not updating" , region )
}
// Make a new session with the new region
oldRegion := f . opt . Region
f . opt . Region = region
c , ses , err := s3Connection ( & f . opt )
if err != nil {
return errors . Wrap ( err , "creating new session failed" )
}
f . c = c
f . ses = ses
fs . Logf ( f , "Switched region to %q from %q" , region , oldRegion )
return nil
}
2016-04-21 21:06:21 +02:00
// listFn is called from list to handle an object.
type listFn func ( remote string , object * s3 . Object , isDirectory bool ) error
2019-08-09 12:29:36 +02:00
// list lists the objects into the function supplied from
// the bucket and directory supplied. The remote has prefix
// removed from it and if addBucket is set then it adds the
// bucket to the start.
2016-04-23 22:46:52 +02:00
//
2017-06-11 23:43:31 +02:00
// Set recurse to read sub directories
2019-08-09 12:29:36 +02:00
func ( f * Fs ) list ( ctx context . Context , bucket , directory , prefix string , addBucket bool , recurse bool , fn listFn ) error {
if prefix != "" {
prefix += "/"
}
if directory != "" {
directory += "/"
2016-04-23 22:46:52 +02:00
}
2014-05-05 19:25:32 +02:00
delimiter := ""
2017-06-11 23:43:31 +02:00
if ! recurse {
2014-05-05 19:25:32 +02:00
delimiter = "/"
}
2014-12-23 13:09:02 +01:00
var marker * string
2019-09-16 21:25:55 +02:00
// URL encode the listings so we can use control characters in object names
// See: https://github.com/aws/aws-sdk-go/issues/1914
//
// However this doesn't work perfectly under Ceph (and hence DigitalOcean/Dreamhost) because
// it doesn't encode CommonPrefixes.
// See: https://tracker.ceph.com/issues/41870
//
// This does not work under IBM COS also: See https://github.com/rclone/rclone/issues/3345
// though maybe it does on some versions.
//
// This does work with minio but was only added relatively recently
// https://github.com/minio/minio/pull/7265
//
// So we enable only on providers we know supports it properly, all others can retry when a
// XML Syntax error is detected.
var urlEncodeListings = ( f . opt . Provider == "AWS" || f . opt . Provider == "Wasabi" || f . opt . Provider == "Alibaba" )
2015-02-10 18:58:29 +01:00
for {
2014-12-23 13:09:02 +01:00
// FIXME need to implement ALL loop
req := s3 . ListObjectsInput {
2019-09-16 21:25:55 +02:00
Bucket : & bucket ,
Delimiter : & delimiter ,
Prefix : & directory ,
2019-12-26 12:05:00 +01:00
MaxKeys : & f . opt . ListChunk ,
2019-09-16 21:25:55 +02:00
Marker : marker ,
}
if urlEncodeListings {
req . EncodingType = aws . String ( s3 . EncodingTypeUrl )
2014-12-23 13:09:02 +01:00
}
2018-09-03 06:41:04 +02:00
var resp * s3 . ListObjectsOutput
var err error
err = f . pacer . Call ( func ( ) ( bool , error ) {
2019-06-17 10:34:30 +02:00
resp , err = f . c . ListObjectsWithContext ( ctx , & req )
2019-09-16 21:25:55 +02:00
if err != nil && ! urlEncodeListings {
if awsErr , ok := err . ( awserr . RequestFailure ) ; ok {
if origErr := awsErr . OrigErr ( ) ; origErr != nil {
if _ , ok := origErr . ( * xml . SyntaxError ) ; ok {
// Retry the listing with URL encoding as there were characters that XML can't encode
urlEncodeListings = true
req . EncodingType = aws . String ( s3 . EncodingTypeUrl )
fs . Debugf ( f , "Retrying listing because of characters which can't be XML encoded" )
return true , err
}
}
}
}
2019-01-16 14:35:19 +01:00
return f . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2015-02-10 18:58:29 +01:00
if err != nil {
2017-06-11 23:43:31 +02:00
if awsErr , ok := err . ( awserr . RequestFailure ) ; ok {
if awsErr . StatusCode ( ) == http . StatusNotFound {
err = fs . ErrorDirNotFound
}
}
2019-08-09 12:29:36 +02:00
if f . rootBucket == "" {
// if listing from the root ignore wrong region requests returning
// empty directory
if reqErr , ok := err . ( awserr . RequestFailure ) ; ok {
// 301 if wrong region for bucket
if reqErr . StatusCode ( ) == http . StatusMovedPermanently {
fs . Errorf ( f , "Can't change region for bucket %q with no bucket specified" , bucket )
return nil
}
}
}
2016-04-21 21:06:21 +02:00
return err
}
2017-06-11 23:43:31 +02:00
if ! recurse {
2016-04-21 21:06:21 +02:00
for _ , commonPrefix := range resp . CommonPrefixes {
if commonPrefix . Prefix == nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( f , "Nil common prefix received" )
2016-04-21 21:06:21 +02:00
continue
2015-02-10 18:58:29 +01:00
}
2016-04-21 21:06:21 +02:00
remote := * commonPrefix . Prefix
2019-09-16 21:25:55 +02:00
if urlEncodeListings {
remote , err = url . QueryUnescape ( remote )
if err != nil {
fs . Logf ( f , "failed to URL decode %q in listing common prefix: %v" , * commonPrefix . Prefix , err )
continue
}
2019-07-23 13:24:10 +02:00
}
2018-11-02 13:15:30 +01:00
remote = enc . ToStandardPath ( remote )
2019-08-09 12:29:36 +02:00
if ! strings . HasPrefix ( remote , prefix ) {
2017-02-09 12:01:20 +01:00
fs . Logf ( f , "Odd name received %q" , remote )
2016-04-21 21:06:21 +02:00
continue
}
2019-08-09 12:29:36 +02:00
remote = remote [ len ( prefix ) : ]
if addBucket {
remote = path . Join ( bucket , remote )
}
2016-04-21 21:06:21 +02:00
if strings . HasSuffix ( remote , "/" ) {
remote = remote [ : len ( remote ) - 1 ]
}
err = fn ( remote , & s3 . Object { Key : & remote } , true )
if err != nil {
return err
2013-01-08 19:53:35 +01:00
}
}
2016-04-21 21:06:21 +02:00
}
for _ , object := range resp . Contents {
2019-08-09 12:29:36 +02:00
remote := aws . StringValue ( object . Key )
2019-09-16 21:25:55 +02:00
if urlEncodeListings {
remote , err = url . QueryUnescape ( remote )
if err != nil {
fs . Logf ( f , "failed to URL decode %q in listing: %v" , aws . StringValue ( object . Key ) , err )
continue
}
2019-07-23 13:24:10 +02:00
}
2018-11-02 13:15:30 +01:00
remote = enc . ToStandardPath ( remote )
2019-08-09 12:29:36 +02:00
if ! strings . HasPrefix ( remote , prefix ) {
fs . Logf ( f , "Odd name received %q" , remote )
2016-04-21 21:06:21 +02:00
continue
2014-12-23 13:09:02 +01:00
}
2019-08-09 12:29:36 +02:00
remote = remote [ len ( prefix ) : ]
isDirectory := strings . HasSuffix ( remote , "/" )
if addBucket {
remote = path . Join ( bucket , remote )
}
2018-03-19 18:41:46 +01:00
// is this a directory marker?
2019-08-09 12:29:36 +02:00
if isDirectory && object . Size != nil && * object . Size == 0 {
2018-03-19 18:41:46 +01:00
continue // skip directory marker
}
2016-04-21 21:06:21 +02:00
err = fn ( remote , object , false )
if err != nil {
return err
2014-12-23 13:09:02 +01:00
}
2015-02-10 18:58:29 +01:00
}
2016-04-21 21:06:21 +02:00
if ! aws . BoolValue ( resp . IsTruncated ) {
break
}
// Use NextMarker if set, otherwise use last Key
if resp . NextMarker == nil || * resp . NextMarker == "" {
2017-12-20 17:40:41 +01:00
if len ( resp . Contents ) == 0 {
return errors . New ( "s3 protocol error: received listing with IsTruncated set, no NextMarker and no Contents" )
}
2016-04-21 21:06:21 +02:00
marker = resp . Contents [ len ( resp . Contents ) - 1 ] . Key
} else {
marker = resp . NextMarker
}
2019-12-11 18:23:52 +01:00
if urlEncodeListings {
* marker , err = url . QueryUnescape ( * marker )
if err != nil {
return errors . Wrapf ( err , "failed to URL decode NextMarker %q" , * marker )
}
}
2014-05-05 19:25:32 +02:00
}
2016-04-21 21:06:21 +02:00
return nil
2014-05-05 19:25:32 +02:00
}
2017-06-30 11:54:14 +02:00
// Convert a list item into a DirEntry
2019-06-17 10:34:30 +02:00
func ( f * Fs ) itemToDirEntry ( ctx context . Context , remote string , object * s3 . Object , isDirectory bool ) ( fs . DirEntry , error ) {
2017-06-11 23:43:31 +02:00
if isDirectory {
size := int64 ( 0 )
if object . Size != nil {
size = * object . Size
}
2017-06-30 14:37:29 +02:00
d := fs . NewDir ( remote , time . Time { } ) . SetSize ( size )
2017-06-11 23:43:31 +02:00
return d , nil
2016-04-21 21:06:21 +02:00
}
2019-06-17 10:34:30 +02:00
o , err := f . newObjectWithInfo ( ctx , remote , object )
2017-06-11 23:43:31 +02:00
if err != nil {
return nil , err
}
return o , nil
}
// listDir lists files and directories to out
2019-08-09 12:29:36 +02:00
func ( f * Fs ) listDir ( ctx context . Context , bucket , directory , prefix string , addBucket bool ) ( entries fs . DirEntries , err error ) {
2016-04-21 21:06:21 +02:00
// List the objects and directories
2019-08-09 12:29:36 +02:00
err = f . list ( ctx , bucket , directory , prefix , addBucket , false , func ( remote string , object * s3 . Object , isDirectory bool ) error {
2019-06-17 10:34:30 +02:00
entry , err := f . itemToDirEntry ( ctx , remote , object , isDirectory )
2017-06-11 23:43:31 +02:00
if err != nil {
return err
}
if entry != nil {
entries = append ( entries , entry )
2016-04-21 21:06:21 +02:00
}
return nil
} )
if err != nil {
2017-06-11 23:43:31 +02:00
return nil , err
2016-04-21 21:06:21 +02:00
}
2018-03-01 13:11:34 +01:00
// bucket must be present if listing succeeded
2019-08-09 12:29:36 +02:00
f . cache . MarkOK ( bucket )
2017-06-11 23:43:31 +02:00
return entries , nil
2016-04-21 21:06:21 +02:00
}
// listBuckets lists the buckets to out
2019-08-22 22:30:55 +02:00
func ( f * Fs ) listBuckets ( ctx context . Context ) ( entries fs . DirEntries , err error ) {
2016-04-21 21:06:21 +02:00
req := s3 . ListBucketsInput { }
2018-09-03 06:41:04 +02:00
var resp * s3 . ListBucketsOutput
err = f . pacer . Call ( func ( ) ( bool , error ) {
2019-06-17 10:34:30 +02:00
resp , err = f . c . ListBucketsWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return f . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2016-04-21 21:06:21 +02:00
if err != nil {
2017-06-11 23:43:31 +02:00
return nil , err
2016-04-21 21:06:21 +02:00
}
for _ , bucket := range resp . Buckets {
2018-11-02 13:15:30 +01:00
bucketName := enc . ToStandardName ( aws . StringValue ( bucket . Name ) )
2019-08-09 12:29:36 +02:00
f . cache . MarkOK ( bucketName )
d := fs . NewDir ( bucketName , aws . TimeValue ( bucket . CreationDate ) )
2017-06-11 23:43:31 +02:00
entries = append ( entries , d )
2014-05-05 19:25:32 +02:00
}
2017-06-11 23:43:31 +02:00
return entries , nil
2013-01-08 19:53:35 +01:00
}
2017-06-11 23:43:31 +02:00
// List the objects and directories in dir into entries. The
// entries can be returned in any order but should be for a
// complete directory.
//
// dir should be "" to list the root, and should not have
// trailing slashes.
//
// This should return ErrDirNotFound if the directory isn't
// found.
2019-06-17 10:34:30 +02:00
func ( f * Fs ) List ( ctx context . Context , dir string ) ( entries fs . DirEntries , err error ) {
2019-08-09 12:29:36 +02:00
bucket , directory := f . split ( dir )
if bucket == "" {
2019-08-22 22:30:55 +02:00
if directory != "" {
return nil , fs . ErrorListBucketRequired
}
return f . listBuckets ( ctx )
2014-05-05 19:25:32 +02:00
}
2019-08-09 12:29:36 +02:00
return f . listDir ( ctx , bucket , directory , f . rootDirectory , f . rootBucket == "" )
2013-01-23 23:43:20 +01:00
}
2017-06-05 17:14:24 +02:00
// ListR lists the objects and directories of the Fs starting
// from dir recursively into out.
2017-06-11 23:43:31 +02:00
//
// dir should be "" to start from the root, and should not
// have trailing slashes.
//
// This should return ErrDirNotFound if the directory isn't
// found.
//
// It should call callback for each tranche of entries read.
// These need not be returned in any particular order. If
// callback returns an error then the listing will stop
// immediately.
//
// Don't implement this unless you have a more efficient way
2019-08-09 12:29:36 +02:00
// of listing recursively than doing a directory traversal.
2019-06-17 10:34:30 +02:00
func ( f * Fs ) ListR ( ctx context . Context , dir string , callback fs . ListRCallback ) ( err error ) {
2019-08-09 12:29:36 +02:00
bucket , directory := f . split ( dir )
2018-01-12 17:30:54 +01:00
list := walk . NewListRHelper ( callback )
2019-08-09 12:29:36 +02:00
listR := func ( bucket , directory , prefix string , addBucket bool ) error {
return f . list ( ctx , bucket , directory , prefix , addBucket , true , func ( remote string , object * s3 . Object , isDirectory bool ) error {
entry , err := f . itemToDirEntry ( ctx , remote , object , isDirectory )
if err != nil {
return err
}
return list . Add ( entry )
} )
}
if bucket == "" {
2019-08-22 22:30:55 +02:00
entries , err := f . listBuckets ( ctx )
2019-08-09 12:29:36 +02:00
if err != nil {
return err
}
for _ , entry := range entries {
err = list . Add ( entry )
if err != nil {
return err
}
bucket := entry . Remote ( )
err = listR ( bucket , "" , f . rootDirectory , true )
if err != nil {
return err
}
2019-08-22 22:30:55 +02:00
// bucket must be present if listing succeeded
f . cache . MarkOK ( bucket )
2019-08-09 12:29:36 +02:00
}
} else {
err = listR ( bucket , directory , f . rootDirectory , f . rootBucket == "" )
2017-06-11 23:43:31 +02:00
if err != nil {
return err
}
2019-08-22 22:30:55 +02:00
// bucket must be present if listing succeeded
f . cache . MarkOK ( bucket )
2017-06-11 23:43:31 +02:00
}
return list . Flush ( )
2017-06-05 17:14:24 +02:00
}
2016-06-25 22:58:34 +02:00
// Put the Object into the bucket
2019-06-17 10:34:30 +02:00
func ( f * Fs ) Put ( ctx context . Context , in io . Reader , src fs . ObjectInfo , options ... fs . OpenOption ) ( fs . Object , error ) {
2015-11-07 12:14:46 +01:00
// Temporary Object under construction
fs := & Object {
fs : f ,
2016-02-18 12:35:25 +01:00
remote : src . Remote ( ) ,
2015-11-07 12:14:46 +01:00
}
2019-06-17 10:34:30 +02:00
return fs , fs . Update ( ctx , in , src , options ... )
2013-01-08 19:53:35 +01:00
}
2017-09-15 20:20:32 +02:00
// PutStream uploads to the remote path with the modTime given of indeterminate size
2019-06-17 10:34:30 +02:00
func ( f * Fs ) PutStream ( ctx context . Context , in io . Reader , src fs . ObjectInfo , options ... fs . OpenOption ) ( fs . Object , error ) {
return f . Put ( ctx , in , src , options ... )
2017-09-15 20:20:32 +02:00
}
2016-02-24 02:58:55 +01:00
// Check if the bucket exists
2017-06-29 13:26:14 +02:00
//
// NB this can return incorrect results if called immediately after bucket deletion
2019-08-09 12:29:36 +02:00
func ( f * Fs ) bucketExists ( ctx context . Context , bucket string ) ( bool , error ) {
2016-02-24 02:58:55 +01:00
req := s3 . HeadBucketInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
2016-02-24 02:58:55 +01:00
}
2018-09-03 06:41:04 +02:00
err := f . pacer . Call ( func ( ) ( bool , error ) {
2019-06-17 10:34:30 +02:00
_ , err := f . c . HeadBucketWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return f . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2016-02-24 02:58:55 +01:00
if err == nil {
return true , nil
}
if err , ok := err . ( awserr . RequestFailure ) ; ok {
if err . StatusCode ( ) == http . StatusNotFound {
return false , nil
}
}
return false , err
}
2013-01-08 19:53:35 +01:00
// Mkdir creates the bucket if it doesn't exist
2019-06-17 10:34:30 +02:00
func ( f * Fs ) Mkdir ( ctx context . Context , dir string ) error {
2019-08-09 12:29:36 +02:00
bucket , _ := f . split ( dir )
2019-08-22 22:30:55 +02:00
return f . makeBucket ( ctx , bucket )
}
// makeBucket creates the bucket if it doesn't exist
func ( f * Fs ) makeBucket ( ctx context . Context , bucket string ) error {
2019-08-09 12:29:36 +02:00
return f . cache . Create ( bucket , func ( ) error {
req := s3 . CreateBucketInput {
Bucket : & bucket ,
ACL : & f . opt . BucketACL ,
2017-06-29 13:26:14 +02:00
}
2019-08-09 12:29:36 +02:00
if f . opt . LocationConstraint != "" {
req . CreateBucketConfiguration = & s3 . CreateBucketConfiguration {
LocationConstraint : & f . opt . LocationConstraint ,
}
2017-06-29 13:26:14 +02:00
}
2019-08-09 12:29:36 +02:00
err := f . pacer . Call ( func ( ) ( bool , error ) {
_ , err := f . c . CreateBucketWithContext ( ctx , & req )
return f . shouldRetry ( err )
} )
if err == nil {
fs . Infof ( f , "Bucket %q created with ACL %q" , bucket , f . opt . BucketACL )
2014-12-23 13:09:02 +01:00
}
2019-08-09 12:29:36 +02:00
if err , ok := err . ( awserr . Error ) ; ok {
if err . Code ( ) == "BucketAlreadyOwnedByYou" {
err = nil
}
2013-01-08 23:31:16 +01:00
}
2019-08-09 12:29:36 +02:00
return nil
} , func ( ) ( bool , error ) {
return f . bucketExists ( ctx , bucket )
} )
2013-01-08 19:53:35 +01:00
}
2015-11-07 16:31:04 +01:00
// Rmdir deletes the bucket if the fs is at the root
2013-01-08 19:53:35 +01:00
//
// Returns an error if it isn't empty
2019-06-17 10:34:30 +02:00
func ( f * Fs ) Rmdir ( ctx context . Context , dir string ) error {
2019-08-09 12:29:36 +02:00
bucket , directory := f . split ( dir )
if bucket == "" || directory != "" {
2015-11-07 16:31:04 +01:00
return nil
}
2019-08-09 12:29:36 +02:00
return f . cache . Remove ( bucket , func ( ) error {
req := s3 . DeleteBucketInput {
Bucket : & bucket ,
}
err := f . pacer . Call ( func ( ) ( bool , error ) {
_ , err := f . c . DeleteBucketWithContext ( ctx , & req )
return f . shouldRetry ( err )
} )
if err == nil {
fs . Infof ( f , "Bucket %q deleted" , bucket )
}
return err
2018-09-03 06:41:04 +02:00
} )
2013-01-08 19:53:35 +01:00
}
2015-09-22 19:47:16 +02:00
// Precision of the remote
2015-11-07 12:14:46 +01:00
func ( f * Fs ) Precision ( ) time . Duration {
2013-01-19 00:21:02 +01:00
return time . Nanosecond
}
2018-01-23 11:50:50 +01:00
// pathEscape escapes s as for a URL path. It uses rest.URLPathEscape
// but also escapes '+' for S3 and Digital Ocean spaces compatibility
func pathEscape ( s string ) string {
return strings . Replace ( rest . URLPathEscape ( s ) , "+" , "%2B" , - 1 )
}
2019-09-09 21:44:50 +02:00
// copy does a server side copy
//
// It adds the boiler plate to the req passed in and calls the s3
// method
2019-10-04 17:49:06 +02:00
func ( f * Fs ) copy ( ctx context . Context , req * s3 . CopyObjectInput , dstBucket , dstPath , srcBucket , srcPath string , srcSize int64 ) error {
2019-09-09 21:44:50 +02:00
req . Bucket = & dstBucket
req . ACL = & f . opt . ACL
req . Key = & dstPath
source := pathEscape ( path . Join ( srcBucket , srcPath ) )
req . CopySource = & source
if f . opt . ServerSideEncryption != "" {
req . ServerSideEncryption = & f . opt . ServerSideEncryption
}
if f . opt . SSEKMSKeyID != "" {
req . SSEKMSKeyId = & f . opt . SSEKMSKeyID
}
if req . StorageClass == nil && f . opt . StorageClass != "" {
req . StorageClass = & f . opt . StorageClass
}
2019-10-04 17:49:06 +02:00
2019-12-02 18:14:57 +01:00
if srcSize >= int64 ( f . opt . CopyCutoff ) {
2019-10-04 17:49:06 +02:00
return f . copyMultipart ( ctx , req , dstBucket , dstPath , srcBucket , srcPath , srcSize )
}
2019-09-09 21:44:50 +02:00
return f . pacer . Call ( func ( ) ( bool , error ) {
_ , err := f . c . CopyObjectWithContext ( ctx , req )
return f . shouldRetry ( err )
} )
}
2019-10-04 17:49:06 +02:00
func calculateRange ( partSize , partIndex , numParts , totalSize int64 ) string {
start := partIndex * partSize
var ends string
if partIndex == numParts - 1 {
2019-12-02 18:00:54 +01:00
if totalSize >= 1 {
ends = strconv . FormatInt ( totalSize - 1 , 10 )
2019-10-04 17:49:06 +02:00
}
} else {
ends = strconv . FormatInt ( start + partSize - 1 , 10 )
}
return fmt . Sprintf ( "bytes=%v-%v" , start , ends )
}
func ( f * Fs ) copyMultipart ( ctx context . Context , req * s3 . CopyObjectInput , dstBucket , dstPath , srcBucket , srcPath string , srcSize int64 ) ( err error ) {
var cout * s3 . CreateMultipartUploadOutput
if err := f . pacer . Call ( func ( ) ( bool , error ) {
var err error
cout , err = f . c . CreateMultipartUploadWithContext ( ctx , & s3 . CreateMultipartUploadInput {
Bucket : & dstBucket ,
Key : & dstPath ,
} )
return f . shouldRetry ( err )
} ) ; err != nil {
return err
}
uid := cout . UploadId
defer func ( ) {
if err != nil {
// We can try to abort the upload, but ignore the error.
_ = f . pacer . Call ( func ( ) ( bool , error ) {
_ , err := f . c . AbortMultipartUploadWithContext ( ctx , & s3 . AbortMultipartUploadInput {
Bucket : & dstBucket ,
Key : & dstPath ,
UploadId : uid ,
RequestPayer : req . RequestPayer ,
} )
return f . shouldRetry ( err )
} )
}
} ( )
2019-12-02 18:14:57 +01:00
partSize := int64 ( f . opt . CopyCutoff )
2019-10-04 17:49:06 +02:00
numParts := ( srcSize - 1 ) / partSize + 1
var parts [ ] * s3 . CompletedPart
for partNum := int64 ( 1 ) ; partNum <= numParts ; partNum ++ {
if err := f . pacer . Call ( func ( ) ( bool , error ) {
partNum := partNum
uploadPartReq := & s3 . UploadPartCopyInput {
Bucket : & dstBucket ,
Key : & dstPath ,
PartNumber : & partNum ,
UploadId : uid ,
CopySourceRange : aws . String ( calculateRange ( partSize , partNum - 1 , numParts , srcSize ) ) ,
// Args copy from req
CopySource : req . CopySource ,
CopySourceIfMatch : req . CopySourceIfMatch ,
CopySourceIfModifiedSince : req . CopySourceIfModifiedSince ,
CopySourceIfNoneMatch : req . CopySourceIfNoneMatch ,
CopySourceIfUnmodifiedSince : req . CopySourceIfUnmodifiedSince ,
CopySourceSSECustomerAlgorithm : req . CopySourceSSECustomerAlgorithm ,
CopySourceSSECustomerKey : req . CopySourceSSECustomerKey ,
CopySourceSSECustomerKeyMD5 : req . CopySourceSSECustomerKeyMD5 ,
RequestPayer : req . RequestPayer ,
SSECustomerAlgorithm : req . SSECustomerAlgorithm ,
SSECustomerKey : req . SSECustomerKey ,
SSECustomerKeyMD5 : req . SSECustomerKeyMD5 ,
}
uout , err := f . c . UploadPartCopyWithContext ( ctx , uploadPartReq )
if err != nil {
return f . shouldRetry ( err )
}
parts = append ( parts , & s3 . CompletedPart {
PartNumber : & partNum ,
ETag : uout . CopyPartResult . ETag ,
} )
return false , nil
} ) ; err != nil {
return err
}
}
return f . pacer . Call ( func ( ) ( bool , error ) {
_ , err := f . c . CompleteMultipartUploadWithContext ( ctx , & s3 . CompleteMultipartUploadInput {
Bucket : & dstBucket ,
Key : & dstPath ,
MultipartUpload : & s3 . CompletedMultipartUpload {
Parts : parts ,
} ,
RequestPayer : req . RequestPayer ,
UploadId : uid ,
} )
return f . shouldRetry ( err )
} )
}
2015-02-14 19:48:08 +01:00
// Copy src to this remote using server side copy operations.
//
// This is stored with the remote path given
//
// It returns the destination Object and a possible error
//
// Will only be called if src.Fs().Name() == f.Name()
//
// If it isn't possible then return fs.ErrorCantCopy
2019-06-17 10:34:30 +02:00
func ( f * Fs ) Copy ( ctx context . Context , src fs . Object , remote string ) ( fs . Object , error ) {
2019-08-09 12:29:36 +02:00
dstBucket , dstPath := f . split ( remote )
2019-08-22 22:30:55 +02:00
err := f . makeBucket ( ctx , dstBucket )
2017-06-28 22:14:53 +02:00
if err != nil {
return nil , err
}
2015-11-07 12:14:46 +01:00
srcObj , ok := src . ( * Object )
2015-02-14 19:48:08 +01:00
if ! ok {
2017-02-09 12:01:20 +01:00
fs . Debugf ( src , "Can't copy - not same remote type" )
2015-02-14 19:48:08 +01:00
return nil , fs . ErrorCantCopy
}
2019-08-09 12:29:36 +02:00
srcBucket , srcPath := srcObj . split ( )
2015-02-14 19:48:08 +01:00
req := s3 . CopyObjectInput {
MetadataDirective : aws . String ( s3 . MetadataDirectiveCopy ) ,
}
2019-10-04 17:49:06 +02:00
err = f . copy ( ctx , & req , dstBucket , dstPath , srcBucket , srcPath , srcObj . Size ( ) )
2015-02-14 19:48:08 +01:00
if err != nil {
return nil , err
}
2019-06-17 10:34:30 +02:00
return f . NewObject ( ctx , remote )
2015-02-14 19:48:08 +01:00
}
2016-01-11 13:39:33 +01:00
// Hashes returns the supported hash sets.
2018-01-12 17:30:54 +01:00
func ( f * Fs ) Hashes ( ) hash . Set {
2018-01-18 21:27:52 +01:00
return hash . Set ( hash . MD5 )
2016-01-11 13:39:33 +01:00
}
2013-01-08 19:53:35 +01:00
// ------------------------------------------------------------
2015-09-22 19:47:16 +02:00
// Fs returns the parent Fs
2016-02-18 12:35:25 +01:00
func ( o * Object ) Fs ( ) fs . Info {
2015-11-07 12:14:46 +01:00
return o . fs
2014-03-28 18:56:04 +01:00
}
// Return a string version
2015-11-07 12:14:46 +01:00
func ( o * Object ) String ( ) string {
2014-03-28 18:56:04 +01:00
if o == nil {
return "<nil>"
}
return o . remote
}
2015-09-22 19:47:16 +02:00
// Remote returns the remote path
2015-11-07 12:14:46 +01:00
func ( o * Object ) Remote ( ) string {
2013-06-27 21:13:07 +02:00
return o . remote
2013-01-08 19:53:35 +01:00
}
2015-05-09 11:37:43 +02:00
var matchMd5 = regexp . MustCompile ( ` ^[0-9a-f] { 32}$ ` )
2016-01-11 13:39:33 +01:00
// Hash returns the Md5sum of an object returning a lowercase hex string
2019-06-17 10:34:30 +02:00
func ( o * Object ) Hash ( ctx context . Context , t hash . Type ) ( string , error ) {
2018-01-18 21:27:52 +01:00
if t != hash . MD5 {
return "" , hash . ErrUnsupported
2016-01-11 13:39:33 +01:00
}
2018-01-06 15:30:10 +01:00
hash := strings . Trim ( strings . ToLower ( o . etag ) , ` " ` )
2015-05-09 11:37:43 +02:00
// Check the etag is a valid md5sum
2018-01-06 15:30:10 +01:00
if ! matchMd5 . MatchString ( hash ) {
2019-06-17 10:34:30 +02:00
err := o . readMetaData ( ctx )
2018-01-06 15:30:10 +01:00
if err != nil {
return "" , err
}
if md5sum , ok := o . meta [ metaMD5Hash ] ; ok {
md5sumBytes , err := base64 . StdEncoding . DecodeString ( * md5sum )
if err != nil {
return "" , err
}
hash = hex . EncodeToString ( md5sumBytes )
} else {
hash = ""
}
2015-05-09 11:37:43 +02:00
}
2018-01-06 15:30:10 +01:00
return hash , nil
2013-01-08 19:53:35 +01:00
}
// Size returns the size of an object in bytes
2015-11-07 12:14:46 +01:00
func ( o * Object ) Size ( ) int64 {
2013-06-27 21:13:07 +02:00
return o . bytes
2013-01-08 19:53:35 +01:00
}
// readMetaData gets the metadata if it hasn't already been fetched
//
// it also sets the info
2019-06-17 10:34:30 +02:00
func ( o * Object ) readMetaData ( ctx context . Context ) ( err error ) {
2013-06-27 21:13:07 +02:00
if o . meta != nil {
2013-01-08 19:53:35 +01:00
return nil
}
2019-08-09 12:29:36 +02:00
bucket , bucketPath := o . split ( )
2014-12-23 13:09:02 +01:00
req := s3 . HeadObjectInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
Key : & bucketPath ,
2014-07-28 23:32:15 +02:00
}
2018-09-03 06:41:04 +02:00
var resp * s3 . HeadObjectOutput
err = o . fs . pacer . Call ( func ( ) ( bool , error ) {
var err error
2019-06-17 10:34:30 +02:00
resp , err = o . fs . c . HeadObjectWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2013-01-08 19:53:35 +01:00
if err != nil {
2016-06-25 22:23:20 +02:00
if awsErr , ok := err . ( awserr . RequestFailure ) ; ok {
if awsErr . StatusCode ( ) == http . StatusNotFound {
return fs . ErrorObjectNotFound
}
}
2013-01-08 19:53:35 +01:00
return err
}
2014-05-16 17:27:53 +02:00
var size int64
// Ignore missing Content-Length assuming it is 0
// Some versions of ceph do this due their apache proxies
2014-12-23 13:09:02 +01:00
if resp . ContentLength != nil {
size = * resp . ContentLength
2013-01-08 19:53:35 +01:00
}
2014-12-23 13:09:02 +01:00
o . etag = aws . StringValue ( resp . ETag )
2013-06-27 21:13:07 +02:00
o . bytes = size
2014-12-23 13:09:02 +01:00
o . meta = resp . Metadata
2019-10-23 09:16:22 +02:00
if o . meta == nil {
o . meta = map [ string ] * string { }
}
2019-09-09 21:44:50 +02:00
o . storageClass = aws . StringValue ( resp . StorageClass )
2014-12-23 13:09:02 +01:00
if resp . LastModified == nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Failed to read last modified from HEAD: %v" , err )
2013-06-27 21:13:07 +02:00
o . lastModified = time . Now ( )
2014-12-23 13:09:02 +01:00
} else {
o . lastModified = * resp . LastModified
2013-01-08 19:53:35 +01:00
}
2016-09-21 23:13:24 +02:00
o . mimeType = aws . StringValue ( resp . ContentType )
2013-01-08 19:53:35 +01:00
return nil
}
// ModTime returns the modification time of the object
//
// It attempts to read the objects mtime and if that isn't present the
// LastModified returned in the http headers
2019-06-17 10:34:30 +02:00
func ( o * Object ) ModTime ( ctx context . Context ) time . Time {
2018-04-13 14:32:17 +02:00
if fs . Config . UseServerModTime {
return o . lastModified
}
2019-06-17 10:34:30 +02:00
err := o . readMetaData ( ctx )
2013-01-08 19:53:35 +01:00
if err != nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Failed to read metadata: %v" , err )
2013-01-08 19:53:35 +01:00
return time . Now ( )
}
// read mtime out of metadata if available
2013-06-27 21:13:07 +02:00
d , ok := o . meta [ metaMtime ]
2014-12-23 13:09:02 +01:00
if ! ok || d == nil {
2017-02-09 12:01:20 +01:00
// fs.Debugf(o, "No metadata")
2013-06-27 21:13:07 +02:00
return o . lastModified
2013-01-08 19:53:35 +01:00
}
2014-12-23 13:09:02 +01:00
modTime , err := swift . FloatStringToTime ( * d )
2013-01-08 19:53:35 +01:00
if err != nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Failed to read mtime from object: %v" , err )
2013-06-27 21:13:07 +02:00
return o . lastModified
2013-01-08 19:53:35 +01:00
}
return modTime
}
2015-09-22 19:47:16 +02:00
// SetModTime sets the modification time of the local fs object
2019-06-17 10:34:30 +02:00
func ( o * Object ) SetModTime ( ctx context . Context , modTime time . Time ) error {
err := o . readMetaData ( ctx )
2013-01-08 23:31:16 +01:00
if err != nil {
2016-03-22 16:07:10 +01:00
return err
2013-01-08 23:31:16 +01:00
}
2014-12-23 13:09:02 +01:00
o . meta [ metaMtime ] = aws . String ( swift . TimeToFloatString ( modTime ) )
2019-09-09 21:44:50 +02:00
// Can't update metadata here, so return this error to force a recopy
if o . storageClass == "GLACIER" || o . storageClass == "DEEP_ARCHIVE" {
return fs . ErrorCantSetModTime
}
2016-01-02 09:58:48 +01:00
2014-12-23 13:09:02 +01:00
// Copy the object to itself to update the metadata
2019-08-09 12:29:36 +02:00
bucket , bucketPath := o . split ( )
2014-12-23 13:09:02 +01:00
req := s3 . CopyObjectInput {
2019-09-09 21:44:50 +02:00
ContentType : aws . String ( fs . MimeType ( ctx , o ) ) , // Guess the content type
2014-12-23 13:09:02 +01:00
Metadata : o . meta ,
2019-09-09 21:44:50 +02:00
MetadataDirective : aws . String ( s3 . MetadataDirectiveReplace ) , // replace metadata with that passed in
2019-06-03 16:28:19 +02:00
}
2019-10-04 17:49:06 +02:00
return o . fs . copy ( ctx , & req , bucket , bucketPath , bucket , bucketPath , o . bytes )
2013-01-08 19:53:35 +01:00
}
2015-09-22 19:47:16 +02:00
// Storable raturns a boolean indicating if this object is storable
2015-11-07 12:14:46 +01:00
func ( o * Object ) Storable ( ) bool {
2013-01-08 19:53:35 +01:00
return true
}
// Open an object for read
2019-06-17 10:34:30 +02:00
func ( o * Object ) Open ( ctx context . Context , options ... fs . OpenOption ) ( in io . ReadCloser , err error ) {
2019-08-09 12:29:36 +02:00
bucket , bucketPath := o . split ( )
2014-12-23 13:09:02 +01:00
req := s3 . GetObjectInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
Key : & bucketPath ,
2014-12-23 13:09:02 +01:00
}
2019-08-06 16:18:08 +02:00
fs . FixRangeOption ( options , o . bytes )
2016-09-10 12:29:57 +02:00
for _ , option := range options {
switch option . ( type ) {
case * fs . RangeOption , * fs . SeekOption :
_ , value := option . Header ( )
req . Range = & value
default :
if option . Mandatory ( ) {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Unsupported mandatory option: %v" , option )
2016-09-10 12:29:57 +02:00
}
}
}
2018-09-03 06:41:04 +02:00
var resp * s3 . GetObjectOutput
err = o . fs . pacer . Call ( func ( ) ( bool , error ) {
var err error
2019-06-17 10:34:30 +02:00
resp , err = o . fs . c . GetObjectWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2017-09-09 14:02:26 +02:00
if err , ok := err . ( awserr . RequestFailure ) ; ok {
if err . Code ( ) == "InvalidObjectState" {
2019-08-09 12:29:36 +02:00
return nil , errors . Errorf ( "Object in GLACIER, restore first: bucket=%q, key=%q" , bucket , bucketPath )
2017-09-09 14:02:26 +02:00
}
}
2014-12-23 13:09:02 +01:00
if err != nil {
return nil , err
}
return resp . Body , nil
2013-01-08 19:53:35 +01:00
}
2019-11-06 11:41:03 +01:00
var warnStreamUpload sync . Once
2014-04-18 18:04:21 +02:00
// Update the Object from in with modTime and size
2019-06-17 10:34:30 +02:00
func ( o * Object ) Update ( ctx context . Context , in io . Reader , src fs . ObjectInfo , options ... fs . OpenOption ) error {
2019-08-09 12:29:36 +02:00
bucket , bucketPath := o . split ( )
2019-08-22 22:30:55 +02:00
err := o . fs . makeBucket ( ctx , bucket )
2017-06-07 15:16:50 +02:00
if err != nil {
return err
}
2019-06-17 10:34:30 +02:00
modTime := src . ModTime ( ctx )
2018-01-06 15:30:10 +01:00
size := src . Size ( )
2016-02-18 12:35:25 +01:00
2018-11-26 22:09:23 +01:00
multipart := size < 0 || size >= int64 ( o . fs . opt . UploadCutoff )
var uploader * s3manager . Uploader
if multipart {
uploader = s3manager . NewUploader ( o . fs . ses , func ( u * s3manager . Uploader ) {
u . Concurrency = o . fs . opt . UploadConcurrency
2019-07-24 11:03:38 +02:00
u . LeavePartsOnError = o . fs . opt . LeavePartsOnError
2018-11-26 22:09:23 +01:00
u . S3 = o . fs . c
u . PartSize = int64 ( o . fs . opt . ChunkSize )
2019-11-06 11:41:03 +01:00
// size can be -1 here meaning we don't know the size of the incoming file. We use ChunkSize
// buffers here (default 5MB). With a maximum number of parts (10,000) this will be a file of
// 48GB which seems like a not too unreasonable limit.
2018-11-26 22:09:23 +01:00
if size == - 1 {
2019-11-06 11:41:03 +01:00
warnStreamUpload . Do ( func ( ) {
fs . Logf ( o . fs , "Streaming uploads using chunk size %v will have maximum file size of %v" ,
o . fs . opt . ChunkSize , fs . SizeSuffix ( u . PartSize * s3manager . MaxUploadParts ) )
} )
2018-11-26 22:09:23 +01:00
return
}
// Adjust PartSize until the number of parts is small enough.
if size / u . PartSize >= s3manager . MaxUploadParts {
// Calculate partition size rounded up to the nearest MB
u . PartSize = ( ( ( size / s3manager . MaxUploadParts ) >> 20 ) + 1 ) << 20
}
} )
}
2014-04-18 18:04:21 +02:00
2014-12-23 13:09:02 +01:00
// Set the mtime in the meta data
metadata := map [ string ] * string {
metaMtime : aws . String ( swift . TimeToFloatString ( modTime ) ) ,
}
2018-11-26 22:09:23 +01:00
// read the md5sum if available for non multpart and if
// disable checksum isn't present.
var md5sum string
2019-12-02 16:15:59 +01:00
if ! multipart && ! o . fs . opt . DisableChecksum {
2019-06-17 10:34:30 +02:00
hash , err := src . Hash ( ctx , hash . MD5 )
2018-01-06 15:30:10 +01:00
if err == nil && matchMd5 . MatchString ( hash ) {
hashBytes , err := hex . DecodeString ( hash )
if err == nil {
2018-11-26 22:09:23 +01:00
md5sum = base64 . StdEncoding . EncodeToString ( hashBytes )
if multipart {
metadata [ metaMD5Hash ] = & md5sum
}
2018-01-06 15:30:10 +01:00
}
}
}
2014-12-23 13:09:02 +01:00
// Guess the content type
2019-06-17 10:34:30 +02:00
mimeType := fs . MimeType ( ctx , src )
2018-11-26 22:09:23 +01:00
if multipart {
req := s3manager . UploadInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
2018-11-26 22:09:23 +01:00
ACL : & o . fs . opt . ACL ,
2019-08-09 12:29:36 +02:00
Key : & bucketPath ,
2018-11-26 22:09:23 +01:00
Body : in ,
ContentType : & mimeType ,
Metadata : metadata ,
//ContentLength: &size,
}
if o . fs . opt . ServerSideEncryption != "" {
req . ServerSideEncryption = & o . fs . opt . ServerSideEncryption
}
if o . fs . opt . SSEKMSKeyID != "" {
req . SSEKMSKeyId = & o . fs . opt . SSEKMSKeyID
}
if o . fs . opt . StorageClass != "" {
req . StorageClass = & o . fs . opt . StorageClass
}
err = o . fs . pacer . CallNoRetry ( func ( ) ( bool , error ) {
2019-06-17 10:34:30 +02:00
_ , err = uploader . UploadWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-11-26 22:09:23 +01:00
} )
if err != nil {
return err
}
} else {
req := s3 . PutObjectInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
2018-11-26 22:09:23 +01:00
ACL : & o . fs . opt . ACL ,
2019-08-09 12:29:36 +02:00
Key : & bucketPath ,
2018-11-26 22:09:23 +01:00
ContentType : & mimeType ,
Metadata : metadata ,
}
if md5sum != "" {
req . ContentMD5 = & md5sum
}
if o . fs . opt . ServerSideEncryption != "" {
req . ServerSideEncryption = & o . fs . opt . ServerSideEncryption
}
if o . fs . opt . SSEKMSKeyID != "" {
req . SSEKMSKeyId = & o . fs . opt . SSEKMSKeyID
}
if o . fs . opt . StorageClass != "" {
req . StorageClass = & o . fs . opt . StorageClass
}
// Create the request
putObj , _ := o . fs . c . PutObjectRequest ( & req )
// Sign it so we can upload using a presigned request.
//
// Note the SDK doesn't currently support streaming to
// PutObject so we'll use this work-around.
url , headers , err := putObj . PresignRequest ( 15 * time . Minute )
if err != nil {
return errors . Wrap ( err , "s3 upload: sign request" )
}
2019-09-21 03:30:45 +02:00
if o . fs . opt . V2Auth && headers == nil {
headers = putObj . HTTPRequest . Header
}
2018-11-26 22:09:23 +01:00
// Set request to nil if empty so as not to make chunked encoding
if size == 0 {
in = nil
}
// create the vanilla http request
httpReq , err := http . NewRequest ( "PUT" , url , in )
if err != nil {
return errors . Wrap ( err , "s3 upload: new request" )
}
2019-09-04 21:21:10 +02:00
httpReq = httpReq . WithContext ( ctx ) // go1.13 can use NewRequestWithContext
2018-11-26 22:09:23 +01:00
// set the headers we signed and the length
httpReq . Header = headers
httpReq . ContentLength = size
err = o . fs . pacer . CallNoRetry ( func ( ) ( bool , error ) {
resp , err := o . fs . srv . Do ( httpReq )
if err != nil {
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-11-26 22:09:23 +01:00
}
body , err := rest . ReadBody ( resp )
if err != nil {
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-11-26 22:09:23 +01:00
}
if resp . StatusCode >= 200 && resp . StatusCode < 299 {
return false , nil
}
err = errors . Errorf ( "s3 upload: %s: %s" , resp . Status , body )
return fserrors . ShouldRetryHTTP ( resp , retryErrorCodes ) , err
} )
if err != nil {
return err
}
2014-07-19 13:37:11 +02:00
}
2014-12-23 13:09:02 +01:00
2014-07-19 13:37:11 +02:00
// Read the metadata from the newly created object
2014-07-20 12:23:05 +02:00
o . meta = nil // wipe old metadata
2019-06-17 10:34:30 +02:00
err = o . readMetaData ( ctx )
2014-04-18 18:04:21 +02:00
return err
}
2013-01-08 19:53:35 +01:00
// Remove an object
2019-06-17 10:34:30 +02:00
func ( o * Object ) Remove ( ctx context . Context ) error {
2019-08-09 12:29:36 +02:00
bucket , bucketPath := o . split ( )
2014-12-23 13:09:02 +01:00
req := s3 . DeleteObjectInput {
2019-08-09 12:29:36 +02:00
Bucket : & bucket ,
Key : & bucketPath ,
2014-12-23 13:09:02 +01:00
}
2018-09-03 06:41:04 +02:00
err := o . fs . pacer . Call ( func ( ) ( bool , error ) {
2019-06-17 10:34:30 +02:00
_ , err := o . fs . c . DeleteObjectWithContext ( ctx , & req )
2019-01-16 14:35:19 +01:00
return o . fs . shouldRetry ( err )
2018-09-03 06:41:04 +02:00
} )
2014-12-23 13:09:02 +01:00
return err
2013-01-08 19:53:35 +01:00
}
2016-09-21 23:13:24 +02:00
// MimeType of an Object if known, "" otherwise
2019-06-17 10:34:30 +02:00
func ( o * Object ) MimeType ( ctx context . Context ) string {
err := o . readMetaData ( ctx )
2016-09-21 23:13:24 +02:00
if err != nil {
2017-02-09 12:01:20 +01:00
fs . Logf ( o , "Failed to read metadata: %v" , err )
2016-09-21 23:13:24 +02:00
return ""
}
return o . mimeType
}
2019-09-09 21:44:50 +02:00
// SetTier performs changing storage class
func ( o * Object ) SetTier ( tier string ) ( err error ) {
ctx := context . TODO ( )
tier = strings . ToUpper ( tier )
bucket , bucketPath := o . split ( )
req := s3 . CopyObjectInput {
MetadataDirective : aws . String ( s3 . MetadataDirectiveCopy ) ,
StorageClass : aws . String ( tier ) ,
}
2019-10-04 17:49:06 +02:00
err = o . fs . copy ( ctx , & req , bucket , bucketPath , bucket , bucketPath , o . bytes )
2019-09-09 21:44:50 +02:00
if err != nil {
return err
}
o . storageClass = tier
return err
}
// GetTier returns storage class as string
func ( o * Object ) GetTier ( ) string {
if o . storageClass == "" {
return "STANDARD"
}
return o . storageClass
}
2013-01-08 19:53:35 +01:00
// Check the interfaces are satisfied
2015-11-07 12:14:46 +01:00
var (
2017-09-15 20:20:32 +02:00
_ fs . Fs = & Fs { }
_ fs . Copier = & Fs { }
_ fs . PutStreamer = & Fs { }
_ fs . ListRer = & Fs { }
_ fs . Object = & Object { }
_ fs . MimeTyper = & Object { }
2019-09-09 21:44:50 +02:00
_ fs . GetTierer = & Object { }
_ fs . SetTierer = & Object { }
2015-11-07 12:14:46 +01:00
)