The GetObjectRequest contains the parameters used for the GetObject operation. For more information about the optional parameters, refer:
Required Parameters: BucketName, Key
Optional Parameters: VersionId, ModifiedSinceDate, UnmodifiedSinceDate, ETagToMatch, ETagToNotMatch, ByteRange
Inheritance: Amazon.S3.Model.S3Request
        // Delete file from the server
        private void DeleteFile(HttpContext context)
        {
            var _getlen = 10;
                var fileName = context.Request["f"];
                var fileExt = fileName.Remove(0,fileName.LastIndexOf('.')).ToLower();
                var hasThumb =  Regex.Match(fileName.ToLower(),AmazonHelper.ImgExtensions()).Success;
                var keyName = GetKeyName(context,HttpUtility.UrlDecode(context.Request["f"]));
                var client = AmazonHelper.GetS3Client();
                var extrequest = new GetObjectRequest()
                                        .WithByteRange(0,_getlen)
                                        .WithKey(keyName)
                                        .WithBucketName(StorageRoot);
                var extresponse = client.GetObject(extrequest);
                var length = extresponse.ContentLength;
                extresponse.Dispose();
                if(length == _getlen + 1){

                    var delrequest = new DeleteObjectRequest()
                                            .WithKey(keyName)
                                            .WithBucketName(StorageRoot);
                    var delresponse = client.DeleteObject(delrequest);
                    delresponse.Dispose();
                    if(hasThumb){
                        try
                        {
                            keyName = keyName.Replace(fileName,"thumbs/" + fileName.Replace(fileExt,".png"));
                            var thumbcheck = new GetObjectRequest()
                                                    .WithByteRange(0,_getlen)
                                                    .WithKey(keyName)
                                                    .WithBucketName(StorageRoot);
                            var thumbCheckResponse = client.GetObject(thumbcheck);
                            length = extresponse.ContentLength;
                            thumbCheckResponse.Dispose();
                            if(length == _getlen + 1){
                                var thumbdelrequest = new DeleteObjectRequest()
                                                        .WithKey(keyName)
                                                        .WithBucketName(StorageRoot);
                                var thumbdelresponse = client.DeleteObject(thumbdelrequest);
                                delresponse.Dispose();
                            }
                        }
                        catch (Exception ex)
                        {

                           var messg = ex.Message;
                        }
                    }

                }
        }
Exemplo n.º 2
0
 public static void ResizeImageAndUpload(AmazonS3 anAmazonS3Client, string aBucketName, string aCurrentPhotoName, string aNewImageName, int aSize)
 {
     GetObjectRequest myGetRequest = new GetObjectRequest().WithBucketName(aBucketName).WithKey(aCurrentPhotoName);
     GetObjectResponse myResponse = anAmazonS3Client.GetObject(myGetRequest);
     Stream myStream = myResponse.ResponseStream;
     ResizeAndUpload(myStream, anAmazonS3Client, aBucketName, aNewImageName, aSize);
 }
        public override byte[] DownloadToByteArray(string container, string fileName)
        {
            client = AWSClientFactory.CreateAmazonS3Client(ExtendedProperties["accessKey"], ExtendedProperties["secretKey"], RegionEndpoint.USEast1);
            String S3_KEY = fileName;

            GetObjectRequest request = new GetObjectRequest()
            {
                BucketName = container,
                Key = S3_KEY,
            };

            GetObjectResponse response = client.GetObject(request);
            int numBytesToRead = (int)response.ContentLength;
            int numBytesRead = 0;
            byte[] buffer = new byte[numBytesToRead];
            while (numBytesToRead > 0)
            {
                int n = response.ResponseStream.Read(buffer, numBytesRead, numBytesToRead);
                if (n == 0)
                    break;
                numBytesRead += n;
                numBytesToRead -= n;
            }

            return buffer;
        }
Exemplo n.º 4
0
 //AWS get an object
 public void GetObject(AmazonS3Client client, string bucketName, String keyName)
 {
     try
     {
         using (client)
         {
             Amazon.S3.Model.GetObjectRequest request = new Amazon.S3.Model.GetObjectRequest
             {
                 BucketName = bucketName,
                 Key        = keyName
             };
             using (GetObjectResponse response = client.GetObject(request))
             {
                 String dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), keyName);
                 if (!File.Exists(dest))
                 {
                     response.WriteResponseStreamToFile(dest);
                 }
             }
         }
     }
     catch
     {
     }
 }
Exemplo n.º 5
0
        public static async Task <Stream> readStreamFromS3ToMemory(string bucketName,
                                                                   string keyName,
                                                                   string bucketRegion,
                                                                   string credential       = @"embeded",
                                                                   AmazonS3Client s3client = null)
        {
            AmazonS3Client client;

            if (credential != @"embeded")
            {
                client = new AmazonS3Client(RegionEndpoint.GetBySystemName(bucketRegion));
            }
            else if (s3client != null)
            {
                client = s3client;
            }
            else
            {
                client = new AmazonS3Client();
            }
            Console.WriteLine("Getting object starts");
            var request = new Amazon.S3.Model.GetObjectRequest
            {
                BucketName = bucketName,
                Key        = keyName
            };

            Console.WriteLine("BucketName: " + bucketName);
            Console.WriteLine("Key: " + keyName);
            GetObjectResponse response = await client.GetObjectAsync(request);

            return(response.ResponseStream);
        }
Exemplo n.º 6
0
        public Stream GetFileIfChangedSince(string s3Url, DateTime lastModified, out DateTime newLastModified)
        {
            S3PathParts path = _pathParser.Parse(s3Url);
            var request = new GetObjectRequest
            {
                BucketName = path.Bucket,
                Key = path.Key,
                ModifiedSinceDate = lastModified
            };

            try
            {
                // Do NOT dispose of the response here because it will dispose of the response stream also
                // (and that is ALL it does). It's a little gross, but I'll accept it because the alternative
                // is to return a custom Stream that will dispose the response when the Stream itself is
                // disposed, which is grosser.
                GetObjectResponse response = _s3Client.GetObject(request);
                newLastModified = response.LastModified;
                return response.ResponseStream;
            }
            catch (AmazonS3Exception e)
            {
                if (e.StatusCode == HttpStatusCode.NotModified)
                {
                    newLastModified = default(DateTime);
                    return null;
                }
                throw;
            }
        }
Exemplo n.º 7
0
        public ActionResult ViewStats(string id)
        {
            using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(AwsAccessKey, AwsSecretAccessKey))
            {
                var request = new GetObjectRequest();
                request.WithBucketName("MyTwitterStats");
                request.WithKey(id + ".json.gz");

                GetObjectResponse response = null;

                try
                {
                    response = client.GetObject(request);
                }
                catch (AmazonS3Exception)
                {
                    //TODO: Log exception.ErrorCode
                    return HttpNotFound();
                }

                using (response)
                using (var stream = response.ResponseStream)
                using (var zipStream = new GZipStream(stream, CompressionMode.Decompress))
                using (var reader = new StreamReader(zipStream))
                {
                    var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
                    var stats = jsonSerializer.Deserialize<Stats>(new JsonTextReader(reader));

                    return View(stats);
                }
            }
        }
Exemplo n.º 8
0
        public async Task <string> GetObject(string key)
        {
            try
            {
                // Create GetObject request
                var request = new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = _bucket,
                    Key        = key
                };

                // Issue GetObject request
                using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))

                    // View GetObject response
                    using (Stream responseStream = response.ResponseStream)
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            return(reader.ReadToEnd());
                        }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error getting object {key} from bucket {_bucket}. Make sure it exists and your bucket is in the same region as this function.");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw;
            }
        }
Exemplo n.º 9
0
        protected GetObjectRequest ConvertToGetObjectRequest(BaseDownloadRequest request)
        {
            GetObjectRequest getRequest = new GetObjectRequest()
            {
                BucketName = request.BucketName,
                Key = request.Key,
                VersionId = request.VersionId
            };
            getRequest.BeforeRequestEvent += this.RequestEventHandler;

            if (request.IsSetModifiedSinceDate())
            {
                getRequest.ModifiedSinceDate = request.ModifiedSinceDate;
            }
            if (request.IsSetUnmodifiedSinceDate())
            {
                getRequest.UnmodifiedSinceDate = request.UnmodifiedSinceDate;
            }

            getRequest.ServerSideEncryptionCustomerMethod = request.ServerSideEncryptionCustomerMethod;
            getRequest.ServerSideEncryptionCustomerProvidedKey = request.ServerSideEncryptionCustomerProvidedKey;
            getRequest.ServerSideEncryptionCustomerProvidedKeyMD5 = request.ServerSideEncryptionCustomerProvidedKeyMD5;

            return getRequest;
        }
Exemplo n.º 10
0
        protected GetObjectRequest ConvertToGetObjectRequest(BaseDownloadRequest request)
        {
            GetObjectRequest getRequest = new GetObjectRequest()
            {
                BucketName = request.BucketName,
                Key = request.Key,
                VersionId = request.VersionId
            };
            ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)getRequest).AddBeforeRequestHandler(this.RequestEventHandler);

            if (request.IsSetModifiedSinceDate())
            {
                getRequest.ModifiedSinceDate = request.ModifiedSinceDate;
            }
            if (request.IsSetUnmodifiedSinceDate())
            {
                getRequest.UnmodifiedSinceDate = request.UnmodifiedSinceDate;
            }

            getRequest.ServerSideEncryptionCustomerMethod = request.ServerSideEncryptionCustomerMethod;
            getRequest.ServerSideEncryptionCustomerProvidedKey = request.ServerSideEncryptionCustomerProvidedKey;
            getRequest.ServerSideEncryptionCustomerProvidedKeyMD5 = request.ServerSideEncryptionCustomerProvidedKeyMD5;

            return getRequest;
        }
Exemplo n.º 11
0
        public S3ObjectAsString GetS3ObjectAsString(S3KeyBuilder s3Key)
        {
            var request = new GetObjectRequest
            {
                BucketName = AppConfigWebValues.Instance.S3BucketName,
                Key = s3Key.FullKey
            };

            var obj = new S3ObjectAsString();
            try
            {
                using (GetObjectResponse response = _client.GetObject(request))
                using (Stream responseStream = response.ResponseStream)
                using (var reader = new StreamReader(responseStream))
                {
                    obj.Key = s3Key.FullKey;
                    obj.ContentBody = reader.ReadToEnd();
                }
            }
            catch (AmazonS3Exception s3x)
            {
                if (s3x.ErrorCode == S3Constants.NoSuchKey)
                    return null;

                throw;
            }

            return obj;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reads a byte array from an S3 bucket
        /// </summary>
        /// <param name="location">The location of the data you want to read</param>
        /// <param name="guid">The guid of the content you're reading</param>
        /// <returns>A byte array interpretation of the data</returns>
        public byte[] ReadByteArray(StorageLocations location, string guid)
        {
            var keyName = string.Format("{0}/{1}.onx", StorageLocationToString(location), guid);
            var request = new GetObjectRequest().WithBucketName(Bucket).WithKey(keyName);

            return VariousFunctions.StreamToByteArray(_client.GetObject(request).ResponseStream);
        }
        public async Task <GetObjectResponse> GetObjectAsync(string bucket,
                                                             string key,
                                                             CancellationToken cancellationToken = default)
        {
            this.Logger.LogDebug($"[{nameof(this.GetObjectAsync)}]");

            this.Logger.LogTrace(JsonConvert.SerializeObject(new { bucket, key }));

            if (string.IsNullOrWhiteSpace(bucket))
            {
                throw new ArgumentNullException(nameof(bucket));
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var request = new Amazon.S3.Model.GetObjectRequest
            {
                BucketName = bucket,
                Key        = key,
            };

            this.Logger.LogTrace(JsonConvert.SerializeObject(value: request));

            var response = await this.Repository.GetObjectAsync(request : request,
                                                                cancellationToken : cancellationToken == default?this.CancellationToken.Token : cancellationToken);

            this.Logger.LogTrace(JsonConvert.SerializeObject(value: response));

            return(response);
        }
Exemplo n.º 14
0
        public override Stream Open()
        {
            //Synchronously download to memory stream
            try {
                var req = new Amazon.S3.Model.GetObjectRequest()
                {
                    BucketName = bucket, Key = key
                };

                using (var s = provider.S3Client.GetObject(req)){
                    return(StreamExtensions.CopyToMemoryStream(s.ResponseStream));
                }
            } catch (AmazonS3Exception se) {
                if (se.StatusCode == System.Net.HttpStatusCode.NotFound || "NoSuchKey".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FileNotFoundException("Amazon S3 file not found", se);
                }
                else if (se.StatusCode == System.Net.HttpStatusCode.Forbidden || "AccessDenied".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FileNotFoundException("Amazon S3 access denied - file may not exist", se);
                }
                else
                {
                    throw;
                }
            }
            return(null);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Reads a string from an S3 bucket
        /// </summary>
        /// <param name="location">The location of the data you want to read</param>
        /// <param name="guid">The guid of the content you're reading</param>
        /// <returns>A string interpretation of the data</returns>
        public string ReadString(StorageLocations location, string guid)
        {
            var keyName = string.Format("{0}/{1}.onx", StorageLocationToString(location), guid);
            var request = new GetObjectRequest().WithBucketName(Bucket).WithKey(keyName);

            return new StreamReader(_client.GetObject(request).ResponseStream, Encoding.ASCII).ReadToEnd();
        }
        public void TestAllFilesExistAndModifiedInOutput()
        {
            try
            {
                Init();

                ListObjectsRequest inputFileObjects;
                String fileKey = null;

                inputFileObjects = new ListObjectsRequest
                {
                    BucketName = DataTransformer.InputBucketName
                };

                ListObjectsResponse listResponse;
                do
                {
                    listResponse = DataTransformer.s3ForStudentBuckets.ListObjects(inputFileObjects);
                    foreach (S3Object obj in listResponse.S3Objects)
                    {
                        fileKey = obj.Key;
                        GetObjectRequest request = new GetObjectRequest()
                        {
                            BucketName = DataTransformer.OutputBucketName,
                            Key = fileKey
                        };
                        GetObjectResponse response = DataTransformer.s3ForStudentBuckets.GetObject(request);

                        StreamReader reader = new StreamReader(response.ResponseStream);
                        string content = reader.ReadToEnd();

                        if(!content.Contains(DataTransformer.JsonComment))
                        {
                            Assert.Fail("Failure - Input file not transformed; output file does not contain JSON comment. " + fileKey);
                        }

                      }

                    // Set the marker property
                    inputFileObjects.Marker = listResponse.NextMarker;
                } while (listResponse.IsTruncated);

            }
            catch (AmazonServiceException ase)
            {
                Console.WriteLine("Error Message:    " + ase.Message);
                Console.WriteLine("HTTP Status Code: " + ase.StatusCode);
                Console.WriteLine("AWS Error Code:   " + ase.ErrorCode);
                Console.WriteLine("Error Type:       " + ase.ErrorType);
                Console.WriteLine("Request ID:       " + ase.RequestId);
            }
            catch (AmazonClientException ace)
            {
                Console.WriteLine("Error Message: " + ace.Message);
            }
        }
Exemplo n.º 17
0
 private static void GetObject(AmazonS3 s3Client, string bucket, string key)
 {
     var getObjectRequest = new GetObjectRequest().WithBucketName(bucket).WithKey(key);
       using (var getObjectResponse = s3Client.GetObject(getObjectRequest))
       {
     var memoryStream = new MemoryStream();
     getObjectResponse.ResponseStream.CopyTo(memoryStream);
     var content = Encoding.Default.GetString(memoryStream.ToArray());
     Console.WriteLine(content);
       }
 }
 public static void Get(string bucket, string key, string fileName)
 {
     AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client();
     FileInfo file = new FileInfo(key);
     Console.WriteLine("Download File " + bucket + ":" + key + " to " + fileName);
     GetObjectRequest get_req = new GetObjectRequest();
     get_req.BucketName = bucket;
     get_req.Key = key;
     GetObjectResponse get_res = s3Client.GetObject(get_req);
     get_res.WriteResponseStreamToFile(fileName);
     Console.WriteLine(get_res.Metadata.AllKeys.FirstOrDefault());
 }
Exemplo n.º 19
0
 void PerformGetRequest(string targetFile, GetObjectRequest request)
 {
     using (var response = httpRetryService.WithRetry(() => s3.GetObject(request)))
     {
         using (var stream = response.ResponseStream)
         {
             using (var outputStream = fileSystem.CreateWriteStream(targetFile))
             {
                 stream.CopyTo(outputStream);
             }
         }
     }
 }
Exemplo n.º 20
0
        public async Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default(CancellationToken)) {
            using (var client = CreateClient()) {
                var req = new GetObjectRequest {
                    BucketName = _bucket,
                    Key = path.Replace('\\', '/')
                };

                var res = await client.GetObjectAsync(req, cancellationToken).AnyContext();
                if (!res.HttpStatusCode.IsSuccessful())
                    return null;
                
                return res.ResponseStream;
            }
        }
        /// <summary>
        /// Determines whether a blob item under the specified location exists.
        /// </summary>
        /// <param name="location">Descriptor of the item on the remote blob storage.</param>
        /// <returns>True if the item exists, otherwise - false</returns>
        public override bool BlobExists(IBlobContentLocation location)
        {
            var request = new GetObjectRequest().WithBucketName(this.bucketName).WithKey(location.FilePath);

            try
            {
                var response = transferUtility.S3Client.GetObject(request);
                return true;
            }
            catch (AmazonS3Exception err)
            {
            }
            return false;
        }
Exemplo n.º 22
0
        private void buttonGetFile_Click(object sender, EventArgs e)
        {
            try
            {
                var config = new AmazonS3Config
                {
                    RegionEndpoint = Amazon.RegionEndpoint.APSoutheast2
                };

                if (this.checkBoxUseProxy.Checked)
                {
                    var proxy = new WebProxy(this.textBoxProxy.Text, int.Parse(this.textBoxProxyPort.Text)) { BypassList = new string[] { textBoxProxyBypass.Text } };
                    WebRequest.DefaultWebProxy = proxy;
                }

                AmazonS3Client amazonS3Client = null;
                if (this.checkBoxUseKeySecret.Checked && this.checkBoxUseToken.Checked == false)
                {
                    amazonS3Client = new AmazonS3Client(this.textBoxKey.Text, this.textBoxSecret.Text, config);
                }
                else if (this.checkBoxUseKeySecret.Checked && this.checkBoxUseToken.Checked)
                {
                    amazonS3Client = new AmazonS3Client(this.textBoxKey.Text, this.textBoxSecret.Text, this.textBoxToken.Text, config);
                }
                else
                {
                    amazonS3Client = new AmazonS3Client(config);
                }

                //textBoxOutput.Text = JsonConvert.SerializeObject(amazonS3Client.Config);

                var request = new GetObjectRequest
                {
                    BucketName = this.textBoxBucket.Text,
                    Key = this.textBoxFile.Text
                };

                using (GetObjectResponse response = amazonS3Client.GetObject(request))
                {
                    using (var reader = new StreamReader(response.ResponseStream))
                    {
                        this.textBoxOutput.Text = reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                this.textBoxOutput.Text = ex.ToString();
            }
        }
Exemplo n.º 23
0
        public virtual void GetFileStream(string bucketName, string keyName, System.IO.Stream target)
        {
            GetObjectRequest objectGetRequest = new GetObjectRequest();
            objectGetRequest.BucketName = bucketName;
            objectGetRequest.Key = keyName;

            using(GetObjectResponse objectGetResponse = m_client.GetObject(objectGetRequest))
            using(System.IO.Stream s = objectGetResponse.ResponseStream)
            {
                try { s.ReadTimeout = (int)TimeSpan.FromMinutes(1).TotalMilliseconds; }
                catch { }

                Utility.Utility.CopyStream(s, target);
            }
        }
Exemplo n.º 24
0
        public static bool Download_File_From_S3(String bucketName, String destination_localPath, String keyName)
        {
            DateTime before = DateTime.Now;

            try
            {
                GetObjectRequest request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);

                using (GetObjectResponse response = s3_client.GetObject(request))
                {
                    string title = response.Metadata["x-amz-meta-title"];
                    if (!File.Exists(destination_localPath))
                    {
                        response.WriteResponseStreamToFile(destination_localPath);
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Caught Exception: " + e.Message);

                Type exType = e.GetType();
                if (exType == typeof(AmazonS3Exception))
                {
                    AmazonS3Exception amazonS3Exception = (AmazonS3Exception)e;
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                        amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                        Console.WriteLine("Please check the provided AWS Credentials.");
                        Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                    }
                    else
                    {
                        Console.WriteLine("An error occurred with the message '{0}' when reading an object", amazonS3Exception.Message);
                    }
                }
                Console.WriteLine("Downloading from s3  Bucket=" + bucketName + " into path=" + destination_localPath + " failed");

                return false;
            }

            TimeSpan downloadTime = DateTime.Now - before;
            Console.WriteLine("Downloading from s3  Bucket=" + bucketName + " into path=" + destination_localPath + " took " + downloadTime.TotalMilliseconds.ToString() + " milliseconds");

            return true;
        }
Exemplo n.º 25
0
        public byte[] RetrieveRequest(string source)
        {
            var key = source.Replace(_baseStorageEndpoint, "");

            var getRequest = new GetObjectRequest()
                .WithBucketName(_configuration.BucketName)
                .WithKey(key);

            var response = _client.GetObject(getRequest);

            using (var memoryStream = new MemoryStream())
            {
                response.ResponseStream.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }
Exemplo n.º 26
0
 public FileData GetFile(string virtualPath) {
   var request = new GetObjectRequest()
     .WithBucketName(this.bucketName)
     .WithKey(FixPathForS3(virtualPath));
   FileData file;
   using (var response = this.s3.GetObject(request)) {
     file = new FileData {
       Name = response.Key.Substring(response.Key.LastIndexOf('/') + 1),
       Updated = DateTime.Now,
       Created = DateTime.Now,
       Length = response.ContentLength,
       VirtualPath = FixPathForN2(response.Key)
     };
   }
   return file;
 }
Exemplo n.º 27
0
        public AmazonS3Object GetFile(string key)
        {
            var request = new GetObjectRequest();
            request.WithBucketName(this._bucketName).WithKey(key);

            try
            {
                var response = this._client.GetObject(request);
                return response.ToAmazonS3Object();
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("Cannot find the file with key {0}. Debug message: {1}", key, ex.Message));
                return null;
            }
        }
Exemplo n.º 28
0
        protected override void ExecuteTask()
        {
            // Ensure the configured bucket exists
            if (!BucketExists(BucketName))
            {
                Project.Log(Level.Error, "[ERROR] S3 Bucket: {0}, not found!", BucketName);
                return;
            }

            // Ensure the file exists
            if (!FileExists(FilePath))
            {
                Project.Log(Level.Error, "File not found: {0}", FilePath);
                return;
            }

            // Get the file from S3
            using (Client)
            {
                try
                {
                    Project.Log(Level.Info, "Downloading \r\n    file: {0}\r\n      as: {1}", FilePath, Outputfile);
                    GetObjectRequest request = new GetObjectRequest
                    {
                        Key = FilePath,
                        BucketName = BucketName,
                        Timeout = timeout
                    };

                    using (var response = Client.GetObject(request))
                    {
                        response.WriteResponseStreamToFile(Outputfile);
                    }
                }
                catch (AmazonS3Exception ex)
                {
                    ShowError(ex);
                }
            }

            // verify that the file actually downloaded
            if (File.Exists(Outputfile))
                Project.Log(Level.Info, "Download successful.", Outputfile);
            else
                Project.Log(Level.Info, "Download FAILED!", Outputfile);
        }
Exemplo n.º 29
0
        public object GetObject(string bucketName, string keyName)
        {
            var request = new GetObjectRequest
            {
                BucketName = bucketName,
                Key = keyName
            };

            using (_client)
            {
                using (var response = _client.GetObject(request))
                {
                    var reader = new StreamReader(response.ResponseStream);
                    return reader.ReadToEnd();
                }
            }
        }
Exemplo n.º 30
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Amazon.S3.Model.GetObjectResponse Get(string key)
        {
            try
            {
                Amazon.S3.AmazonS3Client         client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
                Amazon.S3.Model.GetObjectRequest req    = new Amazon.S3.Model.GetObjectRequest();
                req.BucketName = AWAPI_File_AmazonS3_BucketName;
                req.Key        = key;
                Amazon.S3.Model.GetObjectResponse res = client.GetObject(req);

                return(res);
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
Exemplo n.º 31
0
        public static byte[] GetFile(string name)
        {
            try
            {
                GetObjectRequest request = new GetObjectRequest();
                request.WithBucketName(BucketName)
                    .WithKey(name);

                AmazonS3Config config = new AmazonS3Config();
                config.CommunicationProtocol = Protocol.HTTP;
                using (MemoryStream ms = new MemoryStream())
                using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AccessKeyID, SecretAccessKeyID, config))
                using (GetObjectResponse response = client.GetObject(request))
                {
                    if (response != null)
                    {
                        response.ResponseStream.CopyTo(ms);
                        return ms.ToArray();
                    }
                    else
                        return null;
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
            #if(DEBUG)
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Please check the provided AWS Credentials.");
                    Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("An error occurred with the message '{0}' when deleting an object", amazonS3Exception.Message);
                }
            #endif
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }
Exemplo n.º 32
0
        public bool OpenFile(object FileObject, ITableMetadata TableMeta)
        {
            //throw new NotImplementedException();
            var fileName = (S3Object)FileObject;
            AmazonS3 s3 = AWSClientFactory.CreateAmazonS3Client();

            var gor = new GetObjectRequest().WithBucketName(fileName.BucketName).WithKey(fileName.Key);
            var file = s3.GetObject(gor);
            string text = "";

            using (var ms = new MemoryStream())
            {
                System.Diagnostics.Trace.WriteLine("Started reading file");
                file.ResponseStream.CopyTo(ms); //actually fetches the file from S3
                System.Diagnostics.Trace.WriteLine("Finished reading file");
                using (var gzipStream = new GZipStream(new MemoryStream(ms.ToArray()), CompressionMode.Decompress))
                {
                    System.Diagnostics.Trace.WriteLine("Decompressing file");
                    const int size = 4096;
                    byte[] buffer = new byte[size];
                    using (MemoryStream memory = new MemoryStream())
                    {
                        int count = 0;
                        do
                        {
                            count = gzipStream.Read(buffer, 0, size);
                            if (count > 0)
                            {
                                memory.Write(buffer, 0, count);
                            }
                        }
                        while (count > 0);
                        var memArray = memory.ToArray();
                        text = ASCIIEncoding.ASCII.GetString(memArray);
                    }
                    System.Diagnostics.Trace.WriteLine("Finished decompressing file");
                }
            }

            Lines = text.Split(TableMeta.ColumnDelimiters, StringSplitOptions.RemoveEmptyEntries);
            System.Diagnostics.Trace.WriteLine("Finished reading file");

            lineCount = -1;
            return true;
        }
Exemplo n.º 33
0
        /// <summary>
        /// Check if the file exists or not
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Amazon.S3.Model.GetObjectResponse Get(string fileName)
        {
            try
            {
                Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(ConfigurationLibrary.Config.fileAmazonS3AccessKey,
                                                                               ConfigurationLibrary.Config.fileAmazonS3SecreyKey);
                Amazon.S3.Model.GetObjectRequest req = new Amazon.S3.Model.GetObjectRequest();
                req.BucketName = GetBucketNameFromUrl(fileName); // ConfigurationLibrary.Config.fileAmazonS3BucketName;
                req.Key        = fileName;
                Amazon.S3.Model.GetObjectResponse res = client.GetObject(req);

                return(res);
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
Exemplo n.º 34
0
        protected GetObjectRequest ConvertToGetObjectRequest(BaseDownloadRequest request)
        {
            GetObjectRequest getRequest = new GetObjectRequest()
                .WithBucketName(request.BucketName)
                .WithKey(request.Key)
                .WithTimeout(request.Timeout)
                .WithVersionId(request.VersionId);

            if (request.IsSetModifiedSinceDate())
            {
                getRequest.ModifiedSinceDate = request.ModifiedSinceDate;
            }
            if (request.IsSetUnmodifiedSinceDate())
            {
                getRequest.UnmodifiedSinceDate = request.UnmodifiedSinceDate;
            }

            return getRequest;
        }
Exemplo n.º 35
0
        public byte[] DownloadStream(string bucketName, string keyName)
        {
            var getObjectRequest = new GetObjectRequest()
            {
                BucketName = bucketName,
                Key = keyName
            };

            var getObjectResponse = _client.GetObject(getObjectRequest);
            var s = getObjectResponse.ResponseStream;
            var buffer = new byte[16 * 1026];
            var retStream = new MemoryStream();
            int readStream;
            while ((readStream = s.Read(buffer, 0, buffer.Length)) > 0)
            {
                retStream.Write(buffer, 0, readStream);
            }
            return retStream.ToArray();
        }
Exemplo n.º 36
0
        public async Task<Stream> Load(string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                var request = new GetObjectRequest
                {
                    BucketName = bucket,
                    Key = filename
                };

                var response = await api.GetObjectAsync(request);
                return response.ResponseStream;
            }
        }
Exemplo n.º 37
0
 public static void Main(string[] args)
 {
     // Create a client
     AmazonS3Client client = new AmazonS3Client();
     string time = PutObj(client);
     // Create a GetObject request
     GetObjectRequest getObjRequest = new GetObjectRequest
     {
         BucketName = "com.loofah.photos",
         Key = time
     };
     System.Console.WriteLine(time);
     // Issue request and remember to dispose of the response
     using (GetObjectResponse getObjResponse = client.GetObject(getObjRequest))
     {
         getObjResponse.WriteResponseStreamToFile("C:\\Users\\Ryan\\Pictures\\" + time + ".jpg", false);
     }
     System.Console.Read();
 }
Exemplo n.º 38
0
        public override async Task <Stream> OpenAsync(string virtualPath, NameValueCollection queryString)
        {
            var  path         = ParseAndFilterPath(virtualPath);
            var  time         = Stopwatch.StartNew();
            long bytesFetched = 0;

            //Synchronously download to memory stream
            try {
                var req = new Amazon.S3.Model.GetObjectRequest()
                {
                    BucketName = path.Bucket, Key = path.Key
                };

                using (var s = await S3Client.GetObjectAsync(req)){
                    using (var stream = s.ResponseStream)
                    {
                        var copy = (Stream)await stream.CopyToMemoryStreamAsync();

                        bytesFetched = copy.Length;
                        return(copy);
                    }
                }
            } catch (AmazonS3Exception se) {
                if (se.StatusCode == System.Net.HttpStatusCode.NotFound || "NoSuchKey".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FileNotFoundException("Amazon S3 file not found", se);
                }
                else if (se.StatusCode == System.Net.HttpStatusCode.Forbidden || "AccessDenied".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FileNotFoundException("Amazon S3 access denied - file may not exist", se);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                time.Stop();
                this.ReportReadTicks(time.ElapsedTicks, bytesFetched);
            }
        }
Exemplo n.º 39
0
        //--- Methods ---
        public async Task FunctionHandler(S3Event uploadEvent, ILambdaContext context)
        {
            string bucket    = uploadEvent.Records[0].S3.Bucket.Name;
            string objectKey = uploadEvent.Records[0].S3.Object.Key;

            using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) {
                S3Model.GetObjectRequest request = new S3Model.GetObjectRequest {
                    BucketName = bucket,
                    Key        = objectKey
                };

                using (S3Model.GetObjectResponse response = await client.GetObjectAsync(request))
                    using (Stream responseStream = response.ResponseStream)
                        using (StreamReader reader = new StreamReader(responseStream)) {
                            while (reader.Peek() >= 0)
                            {
                                LambdaLogger.Log($"{reader.ReadLine().Replace("\t", " | ")}");
                            }
                        }
            }
        }
Exemplo n.º 40
0
        public static GetObjectResponse GetFile(AwsCommonParams commonParams,
                                                string bucketName, string filePath)
        {
            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            // This also implements behavior consistent with the
            // edit counterpart routine for verification purposes
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Substring(1);
            }

            using (var s3 = new Amazon.S3.AmazonS3Client(
                       commonParams.ResolveCredentials(),
                       commonParams.RegionEndpoint))
            {
                var s3Requ = new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = bucketName,
                    //Prefix = filePath,
                    Key = filePath,
                };

                //var s3Resp = s3.ListObjects(s3Requ);
                try
                {
                    var s3Resp = s3.GetObject(s3Requ);

                    return(s3Resp);
                }
                catch (AmazonS3Exception ex)
                    when(ex.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }
            }
        }
Exemplo n.º 41
0
        public async Task GetObjectFromS3Async(string bucketName)
        {
            const string keyName = "Calendario 2018 (003).pdf";

            try
            {
                var request = new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };

                string responseBody;

                using (var response = await _client.GetObjectAsync(request))
                    using (var responseStream = response.ResponseStream)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseBody = reader.ReadToEnd();
                        }

                var pathAndFileName = $"C:\\S3Temp\\{keyName}";

                var createText = responseBody;

                File.WriteAllText(pathAndFileName, createText);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when reading an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when reading an object", e.Message);
            }
        }