Files
duplicati/Duplicati/Library/Backend/S3/S3Wrapper.cs
T

233 lines
9.0 KiB
C#
Raw Normal View History

2009-02-10 23:36:15 +00:00
#region Disclaimer / License
2015-01-20 21:44:52 +01:00
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
2009-02-10 23:36:15 +00:00
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
2011-06-23 21:37:14 +00:00
using Amazon.S3;
using Amazon.S3.Model;
2010-06-19 21:08:21 +00:00
using Duplicati.Library.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Duplicati.Library.Backend
{
/// <summary>
2011-06-23 21:37:14 +00:00
/// Helper class that fixes long list support and injects location headers, includes using directives etc.
/// </summary>
2011-06-23 21:37:14 +00:00
public class S3Wrapper : IDisposable
{
private static readonly string LOGTAG = Logging.Log.LogTagFromType<S3Wrapper>();
2010-05-13 18:21:42 +00:00
private const int ITEM_LIST_LIMIT = 1000;
protected readonly string m_locationConstraint;
protected readonly string m_storageClass;
protected AmazonS3Client m_client;
public readonly string DNSHost;
public S3Wrapper(string awsID, string awsKey, string locationConstraint, string servername, string storageClass, bool useSSL, Dictionary<string, string> options)
{
2019-01-06 18:41:57 +01:00
var cfg = new AmazonS3Config
{
UseHttp = !useSSL,
ServiceURL = (useSSL ? "https://" : "http://") + servername,
BufferSize = (int) Utility.Utility.DEFAULT_BUFFER_SIZE
};
2019-01-06 18:41:57 +01:00
foreach (var opt in options.Keys.Where(x => x.StartsWith("s3-ext-", StringComparison.OrdinalIgnoreCase)))
{
var prop = cfg.GetType().GetProperties().FirstOrDefault(x => string.Equals(x.Name, opt.Substring("s3-ext-".Length), StringComparison.OrdinalIgnoreCase));
if (prop != null && prop.CanWrite)
{
if (prop.PropertyType == typeof(bool))
2019-01-06 18:41:57 +01:00
prop.SetValue(cfg, Utility.Utility.ParseBoolOption(options, opt));
else if (prop.PropertyType.IsEnum)
prop.SetValue(cfg, Enum.Parse(prop.PropertyType, options[opt], true));
else if (prop.PropertyType == typeof(int))
prop.SetValue(cfg, int.Parse(options[opt]));
else if (prop.PropertyType == typeof(long))
prop.SetValue(cfg, long.Parse(options[opt]));
else if (prop.PropertyType == typeof(string))
prop.SetValue(cfg, options[opt]);
}
if (prop == null)
2018-06-20 10:54:37 +02:00
Logging.Log.WriteWarningMessage(LOGTAG, "UnsupportedOption", null, "Unsupported option: {0}", opt);
}
2019-01-06 18:41:57 +01:00
m_client = new AmazonS3Client(awsID, awsKey, cfg);
2011-06-23 21:37:14 +00:00
m_locationConstraint = locationConstraint;
m_storageClass = storageClass;
DNSHost = string.IsNullOrWhiteSpace(cfg.ServiceURL) ? null : new Uri(cfg.ServiceURL).Host;
}
2009-08-27 17:53:19 +00:00
public void AddBucket(string bucketName)
{
2019-01-11 13:19:21 +01:00
var request = new PutBucketRequest
2019-01-06 18:41:57 +01:00
{
BucketName = bucketName
};
2011-06-23 21:37:14 +00:00
if (!string.IsNullOrEmpty(m_locationConstraint))
request.BucketRegionName = m_locationConstraint;
2014-12-02 09:22:38 +01:00
m_client.PutBucket(request);
}
2009-01-25 19:33:36 +00:00
public virtual void GetFileStream(string bucketName, string keyName, System.IO.Stream target)
{
2019-01-11 13:19:21 +01:00
var objectGetRequest = new GetObjectRequest
2019-01-06 18:41:57 +01:00
{
BucketName = bucketName,
Key = keyName
};
2014-12-02 09:22:38 +01:00
2019-01-06 18:41:57 +01:00
using (GetObjectResponse objectGetResponse = m_client.GetObject(objectGetRequest))
using(System.IO.Stream s = objectGetResponse.ResponseStream)
{
try { s.ReadTimeout = (int)TimeSpan.FromMinutes(1).TotalMilliseconds; }
catch { }
2011-06-23 21:37:14 +00:00
Utility.Utility.CopyStream(s, target);
}
}
2009-08-27 17:53:19 +00:00
public void GetFileObject(string bucketName, string keyName, string localfile)
2009-01-25 19:33:36 +00:00
{
using (System.IO.FileStream fs = System.IO.File.Open(localfile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
GetFileStream(bucketName, keyName, fs);
}
2009-08-27 17:53:19 +00:00
public void AddFileObject(string bucketName, string keyName, string localfile)
2009-01-25 19:33:36 +00:00
{
using (System.IO.FileStream fs = System.IO.File.Open(localfile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
AddFileStream(bucketName, keyName, fs);
}
public virtual void AddFileStream(string bucketName, string keyName, System.IO.Stream source)
{
2019-01-11 13:19:21 +01:00
var objectAddRequest = new PutObjectRequest
2019-01-06 18:41:57 +01:00
{
BucketName = bucketName,
Key = keyName,
InputStream = source
};
if (!string.IsNullOrWhiteSpace(m_storageClass))
objectAddRequest.StorageClass = new S3StorageClass(m_storageClass);
2014-12-02 09:22:38 +01:00
m_client.PutObject(objectAddRequest);
}
public virtual async Task AddFileStreamAsync(string bucketName, string keyName, System.IO.Stream source, CancellationToken cancelToken)
{
var objectAddRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
InputStream = source
};
if (!string.IsNullOrWhiteSpace(m_storageClass))
objectAddRequest.StorageClass = new S3StorageClass(m_storageClass);
await m_client.PutObjectAsync(objectAddRequest, cancelToken);
}
2009-08-27 17:53:19 +00:00
public void DeleteObject(string bucketName, string keyName)
{
2019-01-11 13:19:21 +01:00
var objectDeleteRequest = new DeleteObjectRequest
2019-01-06 18:41:57 +01:00
{
BucketName = bucketName,
Key = keyName
};
2014-12-02 09:22:38 +01:00
m_client.DeleteObject(objectDeleteRequest);
}
public virtual IEnumerable<IFileEntry> ListBucket(string bucketName, string prefix)
{
bool isTruncated = true;
string filename = null;
//TODO: Figure out if this is the case with AWSSDK too
//Unfortunately S3 sometimes reports duplicate values when requesting more than one page of results
//So, track the files that have already been returned and skip any duplicates.
HashSet<string> alreadyReturned = new HashSet<string>();
2010-05-13 18:21:42 +00:00
//We truncate after ITEM_LIST_LIMIT elements, and then repeat
while (isTruncated)
{
2011-06-23 21:37:14 +00:00
ListObjectsRequest listRequest = new ListObjectsRequest();
listRequest.BucketName = bucketName;
if (!string.IsNullOrEmpty(filename))
listRequest.Marker = filename;
2011-06-23 21:37:14 +00:00
listRequest.MaxKeys = ITEM_LIST_LIMIT;
if (!string.IsNullOrEmpty(prefix))
listRequest.Prefix = prefix;
2014-12-02 09:22:38 +01:00
ListObjectsResponse listResponse = m_client.ListObjects(listRequest);
isTruncated = listResponse.IsTruncated;
filename = listResponse.NextMarker;
foreach (S3Object obj in listResponse.S3Objects)
2011-06-23 21:37:14 +00:00
{
if (alreadyReturned.Add(obj.Key))
{
2018-11-02 21:34:07 +01:00
yield return new Common.IO.FileEntry(
obj.Key,
obj.Size,
obj.LastModified,
obj.LastModified
);
}
}
}
}
2014-08-21 20:59:21 +02:00
public void RenameFile(string bucketName, string source, string target)
{
2019-01-11 13:19:21 +01:00
var copyObjectRequest = new CopyObjectRequest
2019-01-06 18:41:57 +01:00
{
SourceBucket = bucketName,
SourceKey = source,
DestinationBucket = bucketName,
DestinationKey = target
};
2014-08-21 20:59:21 +02:00
2014-12-02 09:22:38 +01:00
m_client.CopyObject(copyObjectRequest);
2014-08-21 20:59:21 +02:00
DeleteObject(bucketName, source);
}
2011-06-23 21:37:14 +00:00
#region IDisposable Members
2011-06-23 21:37:14 +00:00
public void Dispose()
{
if (m_client != null)
m_client.Dispose();
m_client = null;
}
2011-06-23 21:37:14 +00:00
#endregion
}
}