private string GetURL(int expirySeconds, Amazon.S3.HttpVerb httpVerb)
        {
            var request =
                new Amazon.S3.Model.GetPreSignedUrlRequest
            {
                BucketName = _bucketName,
                Key        = _keyName,
                Verb       = httpVerb,
                Expires    = DateTime.UtcNow.AddSeconds(expirySeconds)
            };

            return(_amazonS3.GetPreSignedURL(request));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the s3 signed URL access (for https access).
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="expirationMinutes">The expiration minutes.</param>
        /// <returns></returns>
        public string GetS3SignedUrlAccess(string key, int expirationMinutes)
        {
            Amazon.S3.Model.GetPreSignedUrlRequest request1 = new Amazon.S3.Model.GetPreSignedUrlRequest()
            {
                BucketName = s3Settings.BucketName,
                Key        = key,
                Expires    = DateTime.UtcNow.AddMinutes(expirationMinutes),
                Protocol   = Amazon.S3.Protocol.HTTPS
            };

            string url = s3Client.GetPreSignedURL(request1);

            return(url);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the s3 signed URL access (for https access).
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetS3SignedUrlAccessByHour(string key)
        {
            var utcNow = DateTime.UtcNow;

            Amazon.S3.Model.GetPreSignedUrlRequest request1 = new Amazon.S3.Model.GetPreSignedUrlRequest()
            {
                BucketName = s3Settings.BucketName,
                Key        = key,
                Expires    = DateTime.UtcNow.Date.AddHours(utcNow.Hour + 1),
                Protocol   = Amazon.S3.Protocol.HTTPS
            };

            string url = s3Client.GetPreSignedURL(request1);

            return(url);
        }
Exemplo n.º 4
0
        public async Task <ServiceResult <PresignedURLResponse> > CreatePresignedUrl(string bucketName, string filePath, string contentType)
        {
            var response = new ServiceResult <PresignedURLResponse>();

            if (!(await ConfirmBucketExists(bucketName)))
            {
                response.Error = Shared.ErrorKey.Asset.InvalidBucket;
                return(response);
            }

            var request = new Amazon.S3.Model.GetPreSignedUrlRequest
            {
                BucketName  = bucketName,
                Verb        = HttpVerb.PUT,
                Expires     = DateTime.UtcNow.AddMinutes(5),
                Key         = filePath,
                ContentType = contentType,
            };

            try
            {
                var result        = this.client.GetPreSignedURL(request);
                var correlationId = Svalbard.Utils.MurmurHash2.ComputeHash(result);

                response.Succeed(new PresignedURLResponse(result, correlationId));
                cache.Set(correlationId, result, request.Expires);
            }
            catch (Exception e)
            {
                logger.LogError(e, "Failed to create a presigned url.");
                response.Error = Shared.ErrorKey.Asset.PresignedRequestFailure;

                return(response);
            }

            return(response);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Takes a key and a bucket and converts the
 /// path into a presigned url for access to restricted
 /// S3 content.
 /// </summary>
 /// <param name="bucket">The bucket that the object is in.</param>
 /// <param name="key">The path to the object in S3.</param>
 /// <param name="expiresIn">The amount of minutes from now to expire the URL from.</param>
 /// <param name="protocol">The protocol of the URL to request. Default is HTTP.</param>
 /// <returns></returns>
 public static string PreSignURL(string bucket, string key, double expiresIn = 5, Amazon.S3.Protocol protocol = Amazon.S3.Protocol.HTTP)
 {
     string url = string.Empty;
     using (Amazon.S3.IAmazonS3 client = new Factory().S3Client())
     {
         Amazon.S3.Model.GetPreSignedUrlRequest request = new Amazon.S3.Model.GetPreSignedUrlRequest()
         {
             BucketName = bucket,
             Key = key,
             Protocol = protocol,
             Expires = DateTime.Now.AddMinutes(expiresIn)
         };
         url = client.GetPreSignedURL(request);
     }
     return url;
 }
        /// <summary>
        /// Uploads the drawing to Amazon S3
        /// </summary>
        /// <param name="dwgFilePath"></param>
        /// <returns>Presigned Url of the uploaded drawing file in Amazon S3</returns>
        public static String UploadDrawingToS3(String dwgFilePath)
        {
            String s3URL = String.Empty;

            try
            {
                if (!System.IO.File.Exists(dwgFilePath))
                {
                    return(s3URL);
                }

                String keyName = System.IO.Path.GetFileName(dwgFilePath);

                //be sure to connect to the endpoint which is the same region of your bucket!
                using (Amazon.S3.IAmazonS3 client = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.USWest2))
                {
                    Amazon.S3.Model.PutObjectRequest putRequest1 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName  = S3BucketName,
                        Key         = keyName,
                        ContentBody = "sample text"
                    };

                    Amazon.S3.Model.PutObjectResponse response1 = client.PutObject(putRequest1);

                    Amazon.S3.Model.PutObjectRequest putRequest2 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName  = S3BucketName,
                        Key         = keyName,
                        FilePath    = dwgFilePath,
                        ContentType = "application/acad"
                    };
                    putRequest2.Metadata.Add("x-amz-meta-title", keyName);

                    Amazon.S3.Model.PutObjectResponse response2 = client.PutObject(putRequest2);

                    Amazon.S3.Model.GetPreSignedUrlRequest request1 = new Amazon.S3.Model.GetPreSignedUrlRequest
                    {
                        BucketName = S3BucketName,
                        Key        = keyName,
                        Expires    = DateTime.Now.AddMinutes(5)
                    };

                    s3URL = client.GetPreSignedURL(request1);

                    Console.WriteLine(s3URL);
                }
            }
            catch (Amazon.S3.AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine("For service sign up go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message);
                }
            }
            return(s3URL);
        }
        /// <summary>
        /// Uploads the drawing to Amazon S3
        /// </summary>
        /// <param name="dwgFilePath"></param>
        /// <returns>Presigned Url of the uploaded drawing file in Amazon S3</returns>
        public static String UploadDrawingToS3(String dwgFilePath)
        {
            String s3URL = String.Empty;

            try
            {
                if (!System.IO.File.Exists(dwgFilePath))
                    return s3URL;

                String keyName = System.IO.Path.GetFileName(dwgFilePath);

                using (Amazon.S3.IAmazonS3 client = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1))
                {
                    Amazon.S3.Model.PutObjectRequest putRequest1 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        ContentBody = "sample text"
                    };

                    Amazon.S3.Model.PutObjectResponse response1 = client.PutObject(putRequest1);

                    Amazon.S3.Model.PutObjectRequest putRequest2 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        FilePath = dwgFilePath,
                        ContentType = "application/acad"
                    };
                    putRequest2.Metadata.Add("x-amz-meta-title", keyName);

                    Amazon.S3.Model.PutObjectResponse response2 = client.PutObject(putRequest2);

                    Amazon.S3.Model.GetPreSignedUrlRequest request1 = new Amazon.S3.Model.GetPreSignedUrlRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        Expires = DateTime.Now.AddMinutes(5)
                    };

                    s3URL = client.GetPreSignedURL(request1);

                    Console.WriteLine(s3URL);
                }
            }
            catch (Amazon.S3.AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine("For service sign up go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message);
                }
            }
            return s3URL;
        }
Exemplo n.º 8
0
        //TODO: See if this works for streaming private files
        //http://anthonyvscode.com/2011/01/11/using-amazon-s3-cloudfront-to-distribute-your-private-files/
        //public string StoreStreamingFile(Stream stream, string contentType)
        //{
        //    string key = Guid.NewGuid().ToString("N") + "." + contentType.Split('/').LastOrDefault();
        //if (stream.Length > 100000000)
        //{
        //    TransferUtility transferUtility = new TransferUtility(S3Helper.S3ClientInstance);
        //    TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest();
        //    uploadRequest.WithBucketName(Helper.AppConfig.AWS_BucketStreaming)
        //        .WithKey(key)
        //        .WithCannedACL(Amazon.S3.Model.S3CannedACL.PublicRead)
        //        .WithAutoCloseStream(true)
        //        .WithContentType(contentType)
        //        .WithStorageClass(Amazon.S3.Model.S3StorageClass.ReducedRedundancy)
        //        .WithPartSize(10485760)
        //        .WithTimeout(3600000)
        //        .WithInputStream(stream);
        //    transferUtility.Upload(uploadRequest);
        //}
        //else
        //{
        //Amazon.S3.Model.PutObjectRequest por = new Amazon.S3.Model.PutObjectRequest();
        //por.WithBucketName(Helper.AppConfig.AWS_Bucket)
        //    .WithKey(key)
        //    .WithCannedACL(Amazon.S3.Model.S3CannedACL.PublicRead)
        //    .WithContentType(contentType)
        //    .WithStorageClass(Amazon.S3.Model.S3StorageClass.ReducedRedundancy)
        //    .WithTimeout(3600000)
        //    .WithInputStream(stream);
        //Amazon.S3.Model.PutObjectResponse response = S3Helper.S3ClientInstance.PutObject(por);
        //}
        //    return key;
        //}
        public string GetFileUrl(string key, bool isSSL)
        {
            DateTime expiration = DateTime.UtcNow.AddDays(1);

            Amazon.S3.Model.GetPreSignedUrlRequest req = new Amazon.S3.Model.GetPreSignedUrlRequest();
            req.WithBucketName(Helper.AppConfig.AWS_Bucket)
                .WithKey(key)
                .WithExpires(expiration)
                .WithVerb(Amazon.S3.Model.HttpVerb.GET);

            return S3Helper.S3ClientInstance.GetPreSignedURL(req);

            if (isSSL)
            {
                req.Protocol = Amazon.S3.Model.Protocol.HTTPS;
            }
            else
            {
                req.Protocol = Amazon.S3.Model.Protocol.HTTP;
            }

            return S3Helper.S3ClientInstance.GetPreSignedURL(req);
        }