コード例 #1
0
        public ICreateLinkRequest BuildCreateLinkRequest(string collection, string relativePath, FileFingerprint fileFingerprint, string existingETag)
        {
            var link = '/' + _pathManager.GetBlobPath(fileFingerprint);

            byte[] md5Digest;

            using (var md5 = MD5.Create())
            {
                md5Digest = md5.ComputeHash(Encoding.UTF8.GetBytes(link));
            }

            var etag = S3Util.ComputeS3Etag(md5Digest);

            if (!string.IsNullOrEmpty(existingETag))
            {
                var match = string.Equals(etag, existingETag, StringComparison.CurrentCultureIgnoreCase);

                if (match)
                {
                    //Debug.WriteLine($"{collection} \"{relativePath}\" already exists and equals {link}");

                    return(null);
                }
            }

            var md5DigestString = Convert.ToBase64String(md5Digest);

            var treeKey = _pathManager.GetTreeNamePath(collection, relativePath);

            var request = new PutObjectRequest
            {
                BucketName              = _pathManager.Bucket,
                ContentBody             = link,
                Key                     = treeKey,
                MD5Digest               = md5DigestString,
                WebsiteRedirectLocation = link,
                ContentType             = MimeDetector.Default.GetMimeType(fileFingerprint.FullFilePath),
                StorageClass            = _s3StorageClass,
                Headers                 =
                {
                    ["x-amz-meta-lastModified"] = fileFingerprint.LastModifiedUtc.ToString("O")
                }
            };

            return(new CreateLinkRequest
            {
                Collection = collection,
                RelativePath = relativePath,
                BlobLink = link,
                FileFingerprint = fileFingerprint,
                Request = request,
                ETag = etag
            });
        }
コード例 #2
0
ファイル: S3Blobs.cs プロジェクト: henricj/AwsDedupSync
        public IUploadBlobRequest BuildUploadBlobRequest(Tuple <FileFingerprint, AnnotatedPath> tuple)
        {
            Debug.WriteLine($"S3Blobs.BuildUploadRequest() {tuple.Item1.FullFilePath} ({tuple.Item1.Fingerprint.Key().Substring(0, 12)})");

            var blobKey = _pathManager.GetBlobPath(tuple.Item1);

            var fullFilePath = tuple.Item2.FileInfo.FullName;

            var fileInfo = new FileInfo(fullFilePath);

            var fingerprint = tuple.Item1.Fingerprint;

            if (fileInfo.Length != fingerprint.Size || fileInfo.LastWriteTimeUtc != tuple.Item1.LastModifiedUtc)
            {
                Debug.WriteLine($"BuildUploadRequest(): {fileInfo.FullName} changed {fingerprint.Size} != {fileInfo.Length} || {tuple.Item1.LastModifiedUtc} != {fileInfo.LastWriteTimeUtc} ({tuple.Item1.LastModifiedUtc - fileInfo.LastWriteTimeUtc})");

                return(null);
            }

            var md5Digest = Convert.ToBase64String(fingerprint.Md5);

            var request = new PutObjectRequest
            {
                BucketName = _pathManager.Bucket,
                Key        = blobKey,
                MD5Digest  = md5Digest,
                Headers    =
                {
                    ContentType   = MimeDetector.Default.GetMimeType(fullFilePath),
                    ContentLength = fingerprint.Size,
                    ContentMD5    = md5Digest
                },
                StorageClass            = _s3StorageClass,
                AutoCloseStream         = false,
                AutoResetStreamPosition = false
            };

            request.Headers["x-amz-meta-lastModified"] = tuple.Item1.LastModifiedUtc.ToString("O");
            request.Headers["x-amz-meta-SHA2-256"]     = Convert.ToBase64String(fingerprint.Sha2_256);
            request.Headers["x-amz-meta-SHA3-512"]     = Convert.ToBase64String(fingerprint.Sha3_512);

            if (!string.IsNullOrEmpty(tuple.Item2.Collection))
            {
                request.Headers["x-amz-meta-original-collection"] = tuple.Item2.Collection;
            }

            if (!string.IsNullOrEmpty(tuple.Item2.RelativePath))
            {
                request.Headers["x-amz-meta-original-path"] = tuple.Item2.RelativePath;
            }

            var fileName = Path.GetFileName(fullFilePath);

            if (!string.IsNullOrWhiteSpace(fileName))
            {
                fileName = PathUtil.NormalizeAsciiName(fileName);
                request.Headers["x-amz-meta-original-name"] = fileName;
                request.Headers.ContentDisposition          = "attachment; filename=" + fileName;
            }

            return(new UploadBlobRequest
            {
                Request = request,
                FileFingerprint = tuple.Item1,
                ETag = S3Util.ComputeS3Etag(fingerprint.Md5),
                Key = blobKey
            });
        }