Esempio n. 1
0
        public string GetSecuredUrl(Uri uri)
        {
            CheckUri(uri);

            try
            {
                using (var client = CreateAmazonS3Client())
                {
                    var absolutePath = HttpUtility.UrlDecode(uri.AbsolutePath);
                    var key          = absolutePath.TrimStart('/');
                    var request      = new GetPreSignedUrlRequest();

                    request.WithBucketName(bucketName)
                    .WithKey(key)
                    .WithExpires(DateTime.UtcNow.Add(tokenExpiryTime));

                    var url = client.GetPreSignedURL(request);
                    return(url);
                }
            }
            catch (Exception e)
            {
                throw new StorageException(string.Format("Failed to get signed URL for file. Uri: {0}.", uri), e);
            }
        }
Esempio n. 2
0
        public Uri FetchFileUrl(string sObjectKey)
        {
            AmazonS3 client      = AWSClientFactory.CreateAmazonS3Client(S3ACCESSKEY, S3SECRETKEY);
            string   S3_KEY      = ConfigurationManager.AppSettings["AWSS3KEY"];
            string   BUCKET_NAME = ConfigurationManager.AppSettings["AWSBUCKET"];

            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();

            request.WithBucketName(BUCKET_NAME);
            request.WithKey(sObjectKey);
            request.WithProtocol(Protocol.HTTP);
            request.WithExpires(DateTime.Now.AddMinutes(3));
            return(new Uri(client.GetPreSignedURL(request), UriKind.Absolute));
        }
Esempio n. 3
0
        public string GenerateURL(string objectName)
        {
            AmazonS3 s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSKey, AWSSecretKey);


            string url = "";

            try
            {
                using (s3Client)
                {
                    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                    request.WithBucketName(bucketName);
                    request.WithKey(prefix + objectName);
                    if (expires == 0)
                    {
                        request.WithExpires(DateTime.Now.AddYears(20));
                    }
                    else
                    {
                        request.WithExpires(DateTime.Now.AddDays(expires));
                    }


                    url = s3Client.GetPreSignedURL(request);
                }
            }
            catch (AmazonS3Exception)
            {
            }

            if (!isPrivate)
            {
                url = url.Substring(0, url.IndexOf("?")); //remove QS
            }

            if (!https)
            {
                url = url.Replace(@"https://", @"http://");
            }

            if (shorten)
            {
                //create is.gd url
                string resp = "";
                try
                {
                    string isgd = @"http://is.gd/create.php?format=simple&url=" + System.Uri.EscapeDataString(url);
                    resp = new System.Net.WebClient().DownloadString(isgd);
                }
                catch (Exception) { }
                if (resp != "")
                {
                    url = resp;
                    if (appendExt)
                    {
                        if (System.IO.Path.GetExtension(objectName).ToLower() == "png")
                        {
                            url += "?" + System.IO.Path.GetExtension(objectName).ToLower();
                        }
                        else
                        {
                            url += "?" + System.IO.Path.GetFileName(objectName).ToLower();
                        }
                    }
                }
            }


            return(url);
        }