Exemplo n.º 1
0
        private async Task UploadStreamToS3(Stream inputStream, Guid id)
        {
            if (!string.IsNullOrEmpty(_s3Options.CertificateSubject))
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    delegate(
                        object ssender,
                        X509Certificate certificate,
                        X509Chain chain,
                        SslPolicyErrors sslPolicyErrors)
                {
                    if (certificate.Subject.IndexOf(_s3Options.CertificateSubject) > -1)
                    {
                        return(true);
                    }
                    return(sslPolicyErrors == SslPolicyErrors.None);
                };
            }

            using (var client = _s3StorageClient.GetAmazonS3Client())
            {
                using (var transferUtility = new TransferUtility(client))
                {
                    var s3Folder = _s3Options.DestinationFolder;
                    var req      = new TransferUtilityUploadRequest
                    {
                        BucketName = _s3Options.BucketName
                    };
                    var fileName = s3Folder + "/doc-" + id.ToString();
                    req.Key         = fileName;
                    req.InputStream = inputStream;
                    await transferUtility.UploadAsync(req);
                }
            }
        }
Exemplo n.º 2
0
        private async Task DownloadFromS3(FileMetadata fileMetadata, HttpContext context)
        {
            if (!string.IsNullOrEmpty(_s3Options.CertificateSubject))
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    delegate(
                        object sender,
                        X509Certificate certificate,
                        X509Chain chain,
                        SslPolicyErrors sslPolicyErrors)
                {
                    if (certificate.Subject.IndexOf(_s3Options.CertificateSubject) > -1)
                    {
                        return(true);
                    }
                    return(sslPolicyErrors == SslPolicyErrors.None);
                };
            }

            using (var client = _s3StorageClient.GetAmazonS3Client())
            {
                GetObjectRequest getObjRequest = new GetObjectRequest
                {
                    BucketName = _s3Options.BucketName
                };
                if (string.IsNullOrWhiteSpace(getObjRequest.BucketName))
                {
                    throw new FrameworkException("Missing S3 storage bucket name.");
                }

                var s3Folder = _s3Options.DestinationFolder;
                if (string.IsNullOrWhiteSpace(s3Folder))
                {
                    throw new FrameworkException("Missing S3 folder name.");
                }

                getObjRequest.Key = s3Folder + "/doc-" + fileMetadata.FileContentId.ToString();

                try
                {
                    using (GetObjectResponse getObjResponse = await client.GetObjectAsync(getObjRequest))
                    {
                        fileMetadata.Size = getObjResponse.ContentLength;
                        await Download(fileMetadata, context, getObjResponse.ResponseStream);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("S3 storage error. Error: " + ex.ToString());
                }
            }
        }