DocSpace-client/common/ASC.Data.Backup.Core/Storage/S3BackupStorage.cs

146 lines
4.8 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup.Storage;
internal class S3BackupStorage : IBackupStorage
2020-05-20 15:14:44 +00:00
{
private readonly string _accessKeyId;
private readonly string _secretAccessKey;
private readonly string _bucket;
private readonly string _region;
private readonly ILog _logger;
2022-03-17 10:13:52 +00:00
public S3BackupStorage(ILog logger, string accessKeyId, string secretAccessKey, string bucket, string region)
{
2022-03-17 10:13:52 +00:00
_logger = logger;
_accessKeyId = accessKeyId;
_secretAccessKey = secretAccessKey;
_bucket = bucket;
_region = region;
}
public string Upload(string storageBasePath, string localPath, Guid userId)
2020-05-20 15:14:44 +00:00
{
string key;
2020-05-20 15:14:44 +00:00
if (string.IsNullOrEmpty(storageBasePath))
2020-05-20 15:14:44 +00:00
{
key = "backup/" + Path.GetFileName(localPath);
2020-05-20 15:14:44 +00:00
}
else
2020-05-20 15:14:44 +00:00
{
key = string.Concat(storageBasePath.Trim(new char[] { ' ', '/', '\\' }), "/", Path.GetFileName(localPath));
}
2020-05-20 15:14:44 +00:00
using (var fileTransferUtility = new TransferUtility(_accessKeyId, _secretAccessKey, RegionEndpoint.GetBySystemName(_region)))
{
fileTransferUtility.Upload(
new TransferUtilityUploadRequest
{
BucketName = _bucket,
FilePath = localPath,
StorageClass = S3StorageClass.StandardInfrequentAccess,
2022-04-14 19:42:15 +00:00
PartSize = 6291456, // 6 MB.
Key = key
});
}
2020-05-20 15:14:44 +00:00
return key;
}
2020-05-20 15:14:44 +00:00
public void Download(string storagePath, string targetLocalPath)
{
var request = new GetObjectRequest
2020-05-20 15:14:44 +00:00
{
BucketName = _bucket,
Key = GetKey(storagePath),
};
2020-05-20 15:14:44 +00:00
using var s3 = GetClient();
using var response = s3.GetObjectAsync(request).Result;
2022-04-15 09:08:06 +00:00
response.WriteResponseStreamToFileAsync(targetLocalPath, true, new CancellationToken());
}
2020-05-20 15:14:44 +00:00
public void Delete(string storagePath)
{
using var s3 = GetClient();
s3.DeleteObjectAsync(new DeleteObjectRequest
2020-05-20 15:14:44 +00:00
{
BucketName = _bucket,
Key = GetKey(storagePath)
});
}
2020-05-20 15:14:44 +00:00
public bool IsExists(string storagePath)
{
using var s3 = GetClient();
try
2020-05-20 15:14:44 +00:00
{
var request = new ListObjectsRequest { BucketName = _bucket, Prefix = GetKey(storagePath) };
var response = s3.ListObjectsAsync(request).Result;
2022-02-09 18:33:50 +00:00
return response.S3Objects.Count > 0;
2020-05-20 15:14:44 +00:00
}
catch (AmazonS3Exception ex)
2020-05-20 15:14:44 +00:00
{
_logger.Warn(ex);
2022-02-09 18:33:50 +00:00
return false;
2020-05-20 15:14:44 +00:00
}
}
2020-05-20 15:14:44 +00:00
public string GetPublicLink(string storagePath)
{
using var s3 = GetClient();
2020-05-20 15:14:44 +00:00
return s3.GetPreSignedURL(
new GetPreSignedUrlRequest
{
BucketName = _bucket,
Key = GetKey(storagePath),
Expires = DateTime.UtcNow.AddDays(1),
Verb = HttpVerb.GET
});
}
private string GetKey(string fileName)
{
// return "backup/" + Path.GetFileName(fileName);
return fileName;
}
private AmazonS3Client GetClient()
{
return new AmazonS3Client(_accessKeyId, _secretAccessKey,
new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(_region)
});
2020-05-20 15:14:44 +00:00
}
}