Пример #1
0
        private async Task CreateBucketWithDeploymentZipAsync(AmazonS3Client s3Client, string bucketName)
        {
            // create bucket if it doesn't exist
            var listBucketsResponse = await s3Client.ListBucketsAsync();

            if (listBucketsResponse.Buckets.Find((bucket) => bucket.BucketName == bucketName) == null)
            {
                var putBucketRequest = new PutBucketRequest
                {
                    BucketName = bucketName
                };
                await s3Client.PutBucketAsync(putBucketRequest);
            }

            // write or overwrite deployment package
            var putObjectRequest = new PutObjectRequest
            {
                BucketName = bucketName,
                Key        = DeploymentZipKey,
                FilePath   = GetDeploymentZipPath()
            };
            await s3Client.PutObjectAsync(putObjectRequest);
        }
Пример #2
0
        public void SimplePutObjectTest()
        {
            var exception            = new AmazonServiceException();
            var mre                  = new AutoResetEvent(false);
            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName  = bucketName,
                Key         = "contentBodyPut" + random.Next(),
                ContentBody = testContent,
                CannedACL   = S3CannedACL.AuthenticatedRead
            };
            PutObjectResponse response = null;

            Client.PutObjectAsync(request, (result) =>
            {
                exception = result.Exception as AmazonServiceException;
                response  = result.Response;
                mre.Set();
            }, options);
            mre.WaitOne();

            Assert.IsTrue(response.ETag.Length > 0);
        }
Пример #3
0
        public async Task <bool> UploadFile(Stream inputStream, string fileName)
        {
            try
            {
                PutObjectRequest request = new PutObjectRequest()
                {
                    InputStream = inputStream,
                    BucketName  = _settings.AWSS3.BucketName,
                    Key         = fileName
                };
                PutObjectResponse response = await _amazonS3.PutObjectAsync(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #4
0
        public static void WriteBinary(
            IAmazonS3 _s3Client,
            string _bucketName,
            string _databaseName,
            string _collectionName,
            string _id,
            string _contentType,
            Stream _dataStream)
        {
            string key = S3NamingHelper.GetBinaryFile(_databaseName, _collectionName, _id);

            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName  = _bucketName,
                Key         = key,
                ContentType = _contentType,
                InputStream = _dataStream
            };

            Task <PutObjectResponse> task = _s3Client.PutObjectAsync(request);

            Task.WaitAll(task);
        }
Пример #5
0
            public void TestGetObjectOk()
            {
                string path = "put_object_ordinary.txt";

                File.WriteAllText(path, "data");
                FileInfo         fileInfo = new FileInfo(path);
                string           key      = "te%%st  ";
                PutObjectRequest request  = new PutObjectRequest()
                {
                    BucketName = this.bucketName,
                    Key        = key,
                    FileInfo   = fileInfo
                };
                String eTag = this.client.PutObject(request).ETAG;

                Assert.AreEqual(eTag, HashUtils.ComputeMD5Hash(fileInfo));
                BosObject bosObject = this.client.GetObject(this.bucketName, key);
                String    content   =
                    Encoding.Default.GetString(IOUtils.StreamToBytes(bosObject.ObjectContent,
                                                                     bosObject.ObjectMetadata.ContentLength, 8192));

                Assert.AreEqual(content, "data");
            }
        public void TestS3OutpostsSigner()
        {
            var signer = new S3Signer();

            var outpostsArn      = "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint";
            var putObjectRequest = new PutObjectRequest()
            {
                BucketName  = outpostsArn,
                Key         = "foo.txt",
                ContentBody = "data"
            };
            var config = new AmazonS3Config
            {
                UseArnRegion   = true,
                RegionEndpoint = RegionEndpoint.USWest2
            };
            var iRequest = S3ArnTestUtils.RunMockRequest(putObjectRequest, PutObjectRequestMarshaller.Instance, config);

            signer.Sign(iRequest, config, new RequestMetrics(), "ACCESS", "SECRET");

            Assert.IsTrue(iRequest.Headers.ContainsKey(HeaderKeys.AuthorizationHeader));
            Assert.IsTrue((iRequest.Headers["Authorization"]).Contains("s3-outposts"));
        }
Пример #7
0
        public static void PutObjectEnhanced(AmazonS3Client s3ForStudentBuckets, string bucketName, string fileKey, string transformedFile)
        {
            Debug.WriteLine(String.Format("RUNNING SOLUTION CODE: {0}! Follow the steps in the lab guide to replace this method with your own implementation.\n", "PutObjectEnhanced"));
            PutObjectRequest putRequest = new PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = fileKey,
                ContentBody = transformedFile,
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256, //這是server side端的加密,下載時還會解密
            };

            putRequest.Metadata.Add("contact", "John Doe"); //在output桶裡面可以看到
            s3ForStudentBuckets.PutObject(putRequest);

            GetObjectMetadataRequest encryptionRequest = new GetObjectMetadataRequest()
            {
                BucketName = bucketName,
                Key        = fileKey,
            };
            ServerSideEncryptionMethod objectEncryption = s3ForStudentBuckets.GetObjectMetadata(encryptionRequest).ServerSideEncryptionMethod;
            GetObjectMetadataResponse  metadataResponse = s3ForStudentBuckets.GetObjectMetadata(encryptionRequest);
            string contactName = metadataResponse.Metadata["x-amz-meta-contact"];
        }
Пример #8
0
        private void UploadToS3(string backupPath, PeriodicBackupSetup localBackupConfigs)
        {
            var awsRegion = RegionEndpoint.GetBySystemName(localBackupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1;

            using (var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, awsRegion))
                using (var fileStream = File.OpenRead(backupPath))
                {
                    var key     = Path.GetFileName(backupPath);
                    var request = new PutObjectRequest();
                    request.WithMetaData("Description", GetArchiveDescription());
                    request.WithInputStream(fileStream);
                    request.WithBucketName(localBackupConfigs.S3BucketName);
                    request.WithKey(key);
                    request.WithTimeout(60 * 60 * 1000);          // 1 hour
                    request.WithReadWriteTimeout(60 * 60 * 1000); // 1 hour

                    using (client.PutObject(request))
                    {
                        logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
                                                  Path.GetFileName(backupPath), localBackupConfigs.S3BucketName, key));
                    }
                }
        }
Пример #9
0
        public void Upload(Stream mediaStream, String remotePath)
        {
            if (remotePath.StartsWith("/"))
            {
                remotePath = remotePath.Substring(1);
            }

            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName  = BucketName,
                CannedACL   = S3CannedACL.PublicRead,
                InputStream = mediaStream,
                Key         = remotePath
            };

            request.Metadata.Add("x-amz-meta-uid", "33");
            request.Metadata.Add("x-amz-meta-gid", "33");
            request.Metadata.Add("x-amz-meta-mode", "33188");
            request.Metadata.Add("x-amz-meta-mtime", (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString());
            request.Metadata.Add("Content-Type", "image/png");

            Client.PutObject(request);
        }
Пример #10
0
        public async Task SavePdfDocument_RemovesFileFromTmp()
        {
            var date       = new DateTime(2018, 03, 02, 12, 45, 27);
            var documentId = date.ToString("O");

            var expectedFilePath = $"/tmp/{documentId}.pdf";

            File.Create(expectedFilePath);

            var expectedPutRequest = new PutObjectRequest
            {
                BucketName  = _testBucketName,
                Key         = $"2018/03/02/{documentId}.pdf",
                ContentType = "application/pdf",
                FilePath    = expectedFilePath
            };

            _mockAmazonS3.Setup(x => x.PutObjectAsync(It.Is(Match(expectedPutRequest)), It.IsAny <CancellationToken>()));

            await _subject.SavePdfDocument(documentId);

            File.Exists(expectedFilePath).Should().BeFalse();
        }
Пример #11
0
        public async Task Write(string source, string directory, string fileName)
        {
            PutObjectResponse response = null;

            using (var stream = System.IO.File.OpenRead(source))
            {
                var request = new PutObjectRequest
                {
                    BucketName  = _fileConfig.Root,
                    Key         = fileName,
                    InputStream = stream,
                    ContentType = "application/octet-stream",
                    CannedACL   = S3CannedACL.PublicRead
                };

                response = await _client.PutObjectAsync(request);
            }

            if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new BadRequestException(fileName);
            }
        }
Пример #12
0
        public void AccessPointAndServiceClientDifferentRegionsUseArnRegionEnabled()
        {
            var accessPointArn = "arn:aws:s3:eu-central-1:000011112222:accesspoint/testpoint";

            var request = new PutObjectRequest
            {
                BucketName  = accessPointArn,
                Key         = "foo.txt",
                ContentBody = "data"
            };

            var config = new AmazonS3Config
            {
                RegionEndpoint = RegionEndpoint.USEast1,
                UseArnRegion   = true
            };

            var internalRequest = RunMockRequest(request, PutObjectRequestMarshaller.Instance, config);

            Assert.AreEqual(new Uri("https://testpoint-000011112222.s3-accesspoint.eu-central-1.amazonaws.com"), internalRequest.Endpoint);
            Assert.AreEqual("/foo.txt", internalRequest.ResourcePath);
            Assert.AreEqual("eu-central-1", internalRequest.AuthenticationRegion);
        }
Пример #13
0
        private static void VerifyPut(string data, PutObjectRequest putRequest)
        {
            var getRequest = new GetObjectRequest
            {
                BucketName = bucketName,
                Key        = putRequest.Key
            };
            string responseData;

            using (var response = Client.GetObject(getRequest))
                using (var responseStream = response.ResponseStream)
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        responseData = reader.ReadToEnd();

                        Assert.AreEqual(data, responseData);
                        if (putRequest.StorageClass != null && putRequest.StorageClass != S3StorageClass.Standard)
                        {
                            Assert.IsNotNull(response.StorageClass);
                            Assert.AreEqual(response.StorageClass, putRequest.StorageClass);
                        }
                    }
        }
Пример #14
0
        public void PutObjectWithBacklashInKey()
        {
            const string writtenContent = @"an object with a \ in the key";
            const string key            = @"My\Key";

            var request = new PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = key,
                ContentBody = writtenContent,
            };

            Client.PutObject(request);

            using (var response = Client.GetObject(new GetObjectRequest {
                BucketName = bucketName, Key = key
            }))
                using (var reader = new StreamReader(response.ResponseStream))
                {
                    var readContent = reader.ReadToEnd();
                    Assert.AreEqual(readContent, writtenContent);
                }
        }
Пример #15
0
        public async Task <string> UploadFileAsync(string filePath, string fileName, Stream stream, string bucketName)
        {
            using (var client = CreateClient())
            {
                string key     = $"{filePath}/{Guid.NewGuid():N}_{fileName}";
                var    request = new PutObjectRequest()
                {
                    BucketName  = bucketName,
                    CannedACL   = S3CannedACL.AuthenticatedRead,
                    Key         = key,
                    InputStream = stream,
                };

                var res = await client.PutObjectAsync(request);

                if (res.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(key);
                }
            }

            return(null);
        }
Пример #16
0
        public void PutObject_StreamAndFile()
        {
            PutObjectRequest request = new PutObjectRequest();

            request.BucketName = bucketName;
            request.Key        = "PutObjectTest";
            using (FileStream fStream = new FileStream("PutObjectFile.txt", FileMode.Open))
            {
                request.InputStream = fStream;
                request.FilePath    = "PutObjectFile.txt";

                try
                {
                    Client.PutObject(request);
                }
                catch (ArgumentException ex)
                {
                    Assert.AreEqual <string>("Please specify one of either an InputStream or a FilePath to be PUT as an S3 object.", ex.Message);
                    Console.WriteLine(ex.ToString());
                    throw ex;
                }
            }
        }
Пример #17
0
        public void TestGetObjectAPMKMS()
        {
            PutObjectRequest putObjectRequest = new PutObjectRequest()
            {
                BucketName  = bucketName,
                Key         = $"key-{Guid.NewGuid()}",
                ContentBody = SampleContent,
            };

            s3EncryptionClientMetadataModeKMS.PutObject(putObjectRequest);

            GetObjectRequest getObjectRequest = new GetObjectRequest
            {
                BucketName = bucketName,
                Key        = putObjectRequest.Key
            };

            AssertExtensions.ExpectException(() =>
            {
                s3EncryptionClientMetadataModeKMS.EndGetObject(
                    s3EncryptionClientMetadataModeKMS.BeginGetObject(getObjectRequest, null, null));
            }, typeof(NotSupportedException), APMKMSErrorRegex);
        }
Пример #18
0
        /// <summary>
        /// A Lambda function to respond to HTTP Get methods from API Gateway
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The list of blogs</returns>
        public async Task <APIGatewayProxyResponse> Get(APIGatewayProxyRequest request, ILambdaContext context)
        {
            context.Logger.LogLine("Get Request\n");

            var   sw        = Stopwatch.StartNew();
            Image testImage = await LoadImage(context);

            sw.Stop(); Logging("Load image time: " + sw.Elapsed.ToString(), context);

            sw.Restart();
            var resizedImage = testImage.Resize(256, 256);

            sw.Stop(); Logging("Resize image time: " + sw.Elapsed.ToString(), context);

            resizedImage.SaveAsJpeg(new FileStream("/tmp/lena_resized.jpg", FileMode.Create));

            var putReq = new PutObjectRequest
            {
                BucketName = "lambda-image-converter",
                Key        = "lena_resized.jpg",
                FilePath   = "/tmp/lena_resized.jpg"
            };

            await s3Client.PutObjectAsync(putReq);

            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = "Hello AWS Serverless",
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                }
            };

            globalSw.Stop(); Logging("Total time: " + globalSw.Elapsed.ToString(), context);
            return(response);
        }
Пример #19
0
        public async Task <bool> UploadFile(Stream file, string fileName, bool isPublic)
        {
            Logger.Trace();

            try
            {
                using (var client = new AmazonS3Client(CommonConstants.AWSAccessKeyID, CommonConstants.AWSSecretKey, Amazon.RegionEndpoint.USWest2))
                {
                    PutObjectRequest request = new PutObjectRequest()
                    {
                        InputStream = file,
                        BucketName  = CommonConstants.AmazonS3Bucket,
                        Key         = fileName
                    };

                    if (isPublic)
                    {
                        request.CannedACL = S3CannedACL.PublicRead;
                    }

                    var response = await client.PutObjectAsync(request);
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    return(false);
                }

                Crashes.TrackError(amazonS3Exception);
            }

            return(true);
        }
        private static async Task DownloadObjectAsync(string base64Key, PutObjectRequest putObjectRequest)
        {
            GetObjectRequest getObjectRequest = new GetObjectRequest
            {
                BucketName = bucketName,
                Key        = keyName,
                // Provide encryption information for the object stored in Amazon S3.
                ServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                ServerSideEncryptionCustomerProvidedKey = base64Key
            };

            using (GetObjectResponse getResponse = await client.GetObjectAsync(getObjectRequest))
                using (StreamReader reader = new StreamReader(getResponse.ResponseStream))
                {
                    string content = reader.ReadToEnd();
                    if (String.Compare(putObjectRequest.ContentBody, content) == 0)
                    {
                        Console.WriteLine("Object content is same as we uploaded");
                    }
                    else
                    {
                        Console.WriteLine("Error...Object content is not same.");
                    }

                    if (getResponse.ServerSideEncryptionCustomerMethod == ServerSideEncryptionCustomerMethod.AES256)
                    {
                        Console.WriteLine("Object encryption method is AES256, same as we set");
                    }
                    else
                    {
                        Console.WriteLine("Error...Object encryption method is not the same as AES256 we set");
                    }

                    // Assert.AreEqual(putObjectRequest.ContentBody, content);
                    // Assert.AreEqual(ServerSideEncryptionCustomerMethod.AES256, getResponse.ServerSideEncryptionCustomerMethod);
                }
        }
Пример #21
0
        internal static bool TryDecryptUpload(this IS3Client s3Client, string bucketName, string key, string filePath, AesCryptoServiceProvider aes, FileMetadata metadata)
        {
            try
            {
                using (var fileStream = FileUtilities.GetReadStream(filePath))
                    using (var cryptoStream = new CryptoStream(fileStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
                        using (var lengthStream = new LengthStream(cryptoStream, metadata.Length))
                        {
                            string md5String = Convert.ToBase64String(metadata.MD5);

                            var request = new PutObjectRequest
                            {
                                BucketName  = bucketName,
                                Key         = key,
                                InputStream = lengthStream,
                                MD5Digest   = md5String
                            };

                            s3Client.PutObjectAsync(request).Wait();
                        }

                return(true);
            }

            catch (Exception exception)
            {
                var processedException = ExceptionUtilities.ProcessAmazonS3Exception(exception, null);

                if (processedException is UserErrorException)
                {
                    throw processedException;
                }

                Logger.LogLine($"Exception: {exception.Message}.");
                return(false);
            }
        }
        public async Task <bool> Save(Stream stream, string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                try // Does parent folder exists?
                {
                    var folderName  = CloudPath.GetDirectoryName(filename);
                    var getResponse = await api.GetObjectMetadataAsync(bucket, folderName + "/");
                }
                catch (AmazonS3Exception ex)
                {
                    if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        return(false);
                    }

                    //status wasn't not found, so throw the exception
                    throw;
                }

                var request = new PutObjectRequest
                {
                    BucketName  = bucket,
                    Key         = filename,
                    InputStream = stream
                };

                var response = await api.PutObjectAsync(request);

                return(response != null);
            }
        }
        public override void Execute()
        {
            int timeout = this._config.DefaultTimeout;

            if (this._fileTransporterRequest.Timeout != 0)
            {
                timeout = this._fileTransporterRequest.Timeout;
            }

            PutObjectRequest putRequest = new PutObjectRequest
            {
                BucketName                 = this._fileTransporterRequest.BucketName,
                Key                        = this._fileTransporterRequest.Key,
                CannedACL                  = this._fileTransporterRequest.CannedACL,
                ContentType                = this._fileTransporterRequest.ContentType,
                FilePath                   = this._fileTransporterRequest.FilePath,
                Timeout                    = timeout,
                StorageClass               = this._fileTransporterRequest.StorageClass,
                AutoCloseStream            = this._fileTransporterRequest.AutoCloseStream,
                ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod,
            };

            putRequest.PutObjectProgressEvent += new EventHandler <PutObjectProgressArgs>(this.putObjectProgressEventCallback);

            putRequest.InputStream = this._fileTransporterRequest.InputStream;

            if (this._fileTransporterRequest.metadata != null && this._fileTransporterRequest.metadata.Count > 0)
            {
                putRequest.WithMetaData(this._fileTransporterRequest.metadata);
            }
            if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0)
            {
                putRequest.AddHeaders(this._fileTransporterRequest.Headers);
            }

            this._s3Client.PutObject(putRequest);
        }
        static async Task WritingAnObjectAsync()
        {
            try
            {
                // 1. Put object-specify only key name for the new object.
                var putRequest1 = new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = keyName1,
                    ContentBody = "sample text"
                };

                PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);

                // 2. Put the object-set ContentType and add metadata.
                var putRequest2 = new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = keyName2,
                    FilePath    = filePath,
                    ContentType = "text/plain"
                };
                putRequest2.Metadata.Add("x-amz-meta-title", "someTitle");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(
                    "Error encountered ***. Message:'{0}' when writing an object"
                    , e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    "Unknown encountered on server. Message:'{0}' when writing an object"
                    , e.Message);
            }
        }
Пример #25
0
        public async Task <IActionResult> Post([FromForm] string nome, [FromForm] string cpf, [FromForm] string usuario, [FromForm] string senha, IFormFile file)
        {
            var memoryStream = new MemoryStream();

            file.CopyTo(memoryStream);

            var request = new PutObjectRequest
            {
                BucketName  = "mega-hacka",
                Key         = file.FileName,
                InputStream = memoryStream,
                ContentType = file.ContentType,
                CannedACL   = S3CannedACL.PublicRead
            };



            var s3Client = new Amazon.S3.AmazonS3Client(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"),
                                                        Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"),
                                                        Amazon.RegionEndpoint.USEast1);

            var response = await s3Client.PutObjectAsync(request);

            var cliente = new Cliente();

            cliente.Nome      = nome;
            cliente.CPF       = cpf;
            cliente.Usuario   = usuario;
            cliente.Senha     = senha;
            cliente.UrlImagem = $"https://mega-hacka.s3.amazonaws.com/{request.Key}";

            await _context.Cliente.AddAsync(cliente);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new { id = cliente.Id }));
        }
Пример #26
0
        public async System.Threading.Tasks.Task PutObjectCancellationTest()
        {
            var    fileName = UtilityMethods.GenerateName(@"CancellationTest\LargeFile");
            string basePath = @"c:\temp\test\";
            var    path     = Path.Combine(basePath, fileName);

            UtilityMethods.GenerateFile(path, 50 * MEG_SIZE);

            var putObjectRequest = new PutObjectRequest
            {
                BucketName = bucketName,
                Key        = "CancellationTest" + random.Next(),
                CannedACL  = S3CannedACL.AuthenticatedRead,
                FilePath   = path
            };

            var cts = new CancellationTokenSource();

            cts.CancelAfter(1000);
            var token = cts.Token;

            try
            {
                await Client.PutObjectAsync(putObjectRequest, token);
            }
            catch (OperationCanceledException exception)
            {
                Assert.AreEqual(token, exception.CancellationToken);
                Assert.AreEqual(true, exception.CancellationToken.IsCancellationRequested);
                return;
            }
            finally
            {
                Directory.Delete(basePath, true);
            }
            Assert.Fail("An OperationCanceledException was not thrown");
        }
Пример #27
0
    protected void UploadFileButton_Click(object sender, EventArgs e)
    {
        string    filePath           = Server.MapPath(VideoUploader.PostedFile.FileName);
        string    existingBucketName = "ccdem";
        string    keyName            = Membership.GetUser().ProviderUserKey.GetHashCode().ToString();
        string    fileName           = UtilityFunctions.GenerateChar() + VideoUploader.PostedFile.FileName;
        IAmazonS3 client;

        using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(System.Web.Configuration.WebConfigurationManager.AppSettings[0].ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings[1].ToString()))
        {
            var stream = VideoUploader.FileContent;

            stream.Position = 0;

            PutObjectRequest request = new PutObjectRequest();
            request.InputStream = stream;
            request.BucketName  = existingBucketName;
            request.CannedACL   = S3CannedACL.PublicRead;
            //if (!UtilityFunctions.S3FolderExists(existingBucketName, keyName))
            //{
            //    UtilityFunctions.CreateFolder(existingBucketName, keyName);
            //}
            request.Key = keyName + "/" + fileName;
            PutObjectResponse response = client.PutObject(request);
        }

        string bucketUrl = "https://s3-us-west-2.amazonaws.com/" + existingBucketName + "/" + keyName + "/" + fileName;

        cloudFrontUrl = cloudFrontUrl + keyName + "/" + fileName;
        TranscoderUtility.Transcode(keyName + "/" + fileName, keyName + "/mob_" + fileName, existingBucketName);
        //lblPath.Text = "<br/>Successfully uploaded into S3:"+bucketUrl + "<br/> Cloudfront distribution url is "+cloudFrontUrl;
        Models.Video video = new Models.Video()
        {
            Url = cloudFrontUrl
        };
        DAL.DataAccessLayer.AddVideo(video, (Guid)Membership.GetUser().ProviderUserKey);
    }
Пример #28
0
        private static string S3Put(string uri, string name)
        {
            string localFilename = @"c:\temp\s3\temp.jpg";

            try
            {
                using (var webClient = new WebClient())
                {
                    webClient.DownloadFile(string.Format("http://www.pourtrait.com{0}", uri), localFilename);
                }

                AmazonS3Client client = new AmazonS3Client();

                var stream = new System.IO.MemoryStream();
                var image  = Image.FromFile(localFilename);
                image.Save(stream, ImageFormat.Bmp);
                stream.Position = 0;

                var s3Name = name.Replace(" ", "").ToLower();

                PutObjectRequest request = new PutObjectRequest()
                {
                    InputStream = stream,
                    BucketName  = "gounce_import",
                    Key         = s3Name,
                    CannedACL   = S3CannedACL.PublicRead
                };

                PutObjectResponse response = client.PutObject(request);

                return(string.Format("https://s3.amazonaws.com/gounce_import/{0}", s3Name));
            }
            catch (Exception e)
            {
                return(string.Empty);
            }
        }
Пример #29
0
        public async Task TestDeleteAsync()
        {
            // Create an object with the S3 Client
            var objectId  = Guid.NewGuid().ToString();
            var objectKey = $"uploads/{objectId}";

            using (var stringStream = new MemoryStream(Encoding.UTF8.GetBytes("hi")))
            {
                PutObjectRequest putRequest = new PutObjectRequest
                {
                    BucketName  = TestFixture.BucketName,
                    Key         = objectKey,
                    ContentType = "text/plain",
                    InputStream = stringStream
                };
                await TestFixture.S3Client.PutObjectAsync(putRequest);
            }

            // Delete with IStorage
            using (var storageSession = await TestFixture.S3Storage.OpenAsync())
            {
                await storageSession.DeleteAsync(objectId);
            }

            // See if it deleted
            try
            {
                var objectPath = Url.Combine(TestFixture.BucketWebsite, objectKey);
                await objectPath.GetAsync();

                Assert.False(true, $"storageSession.DeleteAsync did not remove {objectPath}");
            }
            catch (FlurlHttpException fhe)
            {
                Assert.Equal(HttpStatusCode.NotFound, fhe.Call.HttpStatus);
            }
        }
Пример #30
0
        /// <summary>
        /// Handle the file upload request throwing exceptions only on errors from AWS which is critical enough to fail
        /// the entire deployment i.e. access denied while per file errors will result in warnings.
        /// </summary>
        /// <param name="client">The client to use</param>
        /// <param name="request">The request to send</param>
        /// <param name="errorAction">Action to take on per file error</param>
        private S3UploadResult HandleUploadRequest(AmazonS3Client client, PutObjectRequest request, Action <AmazonS3Exception, string> errorAction)
        {
            try
            {
                if (!ShouldUpload(client, request))
                {
                    Log.Verbose(
                        $"Object key {request.Key} exists for bucket {request.BucketName} with same content hash. Skipping upload.");
                    return(new S3UploadResult(request, Maybe <PutObjectResponse> .None));
                }

                return(new S3UploadResult(request, Maybe <PutObjectResponse> .Some(client.PutObject(request))));
            }
            catch (AmazonS3Exception ex)
            {
                if (ex.ErrorCode == "AccessDenied")
                {
                    throw new PermissionException(
                              "AWS-S3-ERROR-0002: The AWS account used to perform the operation does not have " +
                              "the required permissions to upload to the bucket.\n" +
                              ex.Message + "\n" +
                              "For more information visit https://g.octopushq.com/AwsS3Upload#aws-s3-error-0002");
                }

                if (!perFileUploadErrors.ContainsKey(ex.ErrorCode))
                {
                    throw;
                }
                perFileUploadErrors[ex.ErrorCode](request, ex).Tee((message) => errorAction(ex, message));
                return(new S3UploadResult(request, Maybe <PutObjectResponse> .None));
            }
            catch (ArgumentException exception)
            {
                throw new AmazonFileUploadException($"AWS-S3-ERROR-0003: An error occurred uploading file with bucket key {request.Key} possibly due to metadata.\n" +
                                                    "Metadata:\n" + request.Metadata.Keys.Aggregate(string.Empty, (values, key) => $"{values}'{key}' = '{request.Metadata[key]}'\n"), exception);
            }
        }
 public void Upload( string name, Stream content, FileMetadata FileMetadata )
 {
     using ( AmazonS3 client = AWSClientFactory.CreateAmazonS3Client( this.amazonKey, this.amazonSecret ) ) {
         PutObjectRequest request = new PutObjectRequest {
             BucketName = this.amazonBucket,
             Key = name,
             InputStream = content
         };
         client.PutObject( request );
     }
 }