Пример #1
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;
        }
Пример #2
0
        public void PutString(S3KeyBuilder s3Url, string content)
        {
            var putRequest1 = new PutObjectRequest
            {
                BucketName = AppConfigWebValues.Instance.S3BucketName,
                Key = s3Url.FullKey,
                ContentBody = content
            };

            PutObjectResponse response1 = _client.PutObject(putRequest1);
            if (response1.HttpStatusCode != HttpStatusCode.OK)
                throw new Exception("Put not successful. Status code " + response1.HttpStatusCode);
        }
Пример #3
0
        public void GetSignedUploadUrl()
        {
#if OFF
            var key = new S3KeyBuilder("testcontent","txt");
            _s3Dal.PutString(key, "content for testing");

            var s3Obj = _s3Dal.GetS3ObjectAsString(key);

#endif


            FileInfo fi = new FileInfo(@"C:\zTempDocs\t\JFgray2.png");

            var s = _s3Dal.GeneratePreSignedURL(fi.Name);

            Console.WriteLine("url:" + s);

            UploadS3UsingWebService.UploadObject(s, fi.FullName);

        } 
Пример #4
0
 public void DeleteS3Object(S3KeyBuilder s3Key)
 {
     var deleteObjectRequest = new DeleteObjectRequest
     {
         BucketName = AppConfigWebValues.Instance.S3BucketName,
         Key = s3Key.FullKey
     };
     DeleteObjectResponse response = _client.DeleteObject(deleteObjectRequest);
     if (response.HttpStatusCode != HttpStatusCode.NoContent)
         throw new Exception("Delete not successful. Status code " + response.HttpStatusCode);
 }
Пример #5
0
        public S3ObjectAsImage GetS3ObjectAsImage(S3KeyBuilder s3Key)
        {
            var request = new GetObjectRequest
            {
                BucketName = AppConfigWebValues.Instance.S3BucketName,
                Key = s3Key.FullKey
            };

            var obj = new S3ObjectAsImage();
            try
            {
                var ms = new MemoryStream();
                using (GetObjectResponse response = _client.GetObject(request))
                using (Stream responseStream = response.ResponseStream)
                {
                    obj.ContentType = "image/png"; //Futuredev: adjust to correct content type.

                    var buffer = new byte[8000];
                    int bytesRead = -1;
                    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, bytesRead);
                    }
                    obj.Bytes = ms.ToArray();
                }
            }
            catch (AmazonS3Exception s3x)
            {
                if (s3x.ErrorCode == S3Constants.NoSuchKey)
                    return null;

                throw;
            }

            return obj;
        }
 public S3ObjectAsImage GetUserPicture(int appUserId)
 {
     S3KeyBuilder builder = new S3KeyBuilder(S3ObjType.pic, appUserId.ToString(), S3Constants.png);
     return new S3Dal().GetS3ObjectAsImage(builder);
 }