예제 #1
0
        public async Task <bool> FunctionHandler(FileInfo fileInfo, ILambdaContext context)
        {
            context.Logger.LogLine(JsonConvert.SerializeObject(fileInfo));

            var request = new CopyObjectRequest
            {
                SourceBucket   = fileInfo.Bucket, SourceKey = fileInfo.Key, DestinationBucket = fileInfo.result ? "videos-validation-passed" : "videos-validation-failed",
                DestinationKey = fileInfo.Key
            };

            context.Logger.LogLine("copy object");

            CopyObjectResponse response = await this.s3Client.CopyObjectAsync(request).ConfigureAwait(false);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            context.Logger.LogLine("delete object");

            DeleteObjectResponse objectResponse = await this.s3Client.DeleteObjectAsync(new DeleteObjectRequest { Key = fileInfo.Key, BucketName = fileInfo.Bucket }).ConfigureAwait(false);

            return(objectResponse.HttpStatusCode == HttpStatusCode.OK);
        }
예제 #2
0
        public async Task MoveObjectTest()
        {
            // ARRANGE
            CopyObjectRequest req = new CopyObjectRequest()
            {
                DestinationBucket = destinationBucket,
                SourceBucket      = sourceBucket,
                SourceKey         = "test/file.txt",
                DestinationKey    = "test/file2.txt",
            };

            GetObjectMetadataRequest metaReq = new GetObjectMetadataRequest()
            {
                BucketName = req.SourceBucket,
                Key        = req.SourceKey
            };

            GetObjectMetadataResponse meta = await client.GetObjectMetadataAsync(metaReq);

            // ACT
            CopyObjectResponse response = await client.CopyOrMoveObjectAsync(req, 16777216, true);

            // ASSERT
            Assert.Equal(HttpStatusCode.OK, response.HttpStatusCode);
            Assert.Equal(meta.ETag, response.ETag);
        }
예제 #3
0
        /// <summary>
        /// Renames the asset.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset">The asset.</param>
        /// <param name="newName">The new name.</param>
        /// <returns></returns>
        public override bool RenameAsset(AssetStorageProvider assetStorageProvider, Asset asset, string newName)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, AttributeKeys.RootFolder));

            asset.Key = asset.Key.IsNullOrWhiteSpace() ? rootFolder + asset.Name : asset.Key;
            string bucket = GetAttributeValue(assetStorageProvider, AttributeKeys.Bucket);

            try
            {
                AmazonS3Client client = GetAmazonS3Client(assetStorageProvider);

                CopyObjectRequest copyRequest = new CopyObjectRequest();
                copyRequest.SourceBucket      = bucket;
                copyRequest.DestinationBucket = bucket;
                copyRequest.SourceKey         = asset.Key;
                copyRequest.DestinationKey    = GetPathFromKey(asset.Key) + newName;
                CopyObjectResponse copyResponse = client.CopyObject(copyRequest);
                if (copyResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(false);
                }

                if (DeleteAsset(assetStorageProvider, asset))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }

            return(false);
        }
예제 #4
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the key value from user to copy
            Console.Write("Enter the object key you want to copy: ");
            string key_source = Console.ReadLine();

            // retrieve the key value from user to name copied object
            Console.Write("Enter the object key for the copied object: ");
            string key_target = Console.ReadLine();

            // create the request object
            CopyObjectRequest request = new CopyObjectRequest()
            {
                SourceBucket      = AWSS3Factory.S3_BUCKET,
                SourceKey         = key_source,
                DestinationBucket = AWSS3Factory.S3_BUCKET,
                DestinationKey    = key_target
            };

            // copy the object
            CopyObjectResponse response = s3.CopyObject(request);

            // print out object key/value for validation
            Console.WriteLine(string.Format("Copied object {0}/{1} to {2}/{3}", AWSS3Factory.S3_BUCKET, key_source, AWSS3Factory.S3_BUCKET, key_target));
            Console.ReadLine();
        }
예제 #5
0
        internal void RenameObject(String sourceKey, String destKey)
        {
            CopyObjectRequest request = new CopyObjectRequest()
            {
                CannedACL         = S3CannedACL.PublicRead,
                DestinationBucket = _bucketName,
                DestinationKey    = GetFullPrefix(destKey),
                MetadataDirective = S3MetadataDirective.COPY,
                SourceBucket      = _bucketName,
                SourceKey         = GetFullPrefix(sourceKey)
            };

            S3ItemData destination = GetObject(destKey);

            if (destination != null)
            {
                throw new Exception(String.Format("Destination object with key {0} already exists.", request.DestinationKey));
            }

            CopyObjectResponse response = _S3Client.CopyObject(request);

            if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception(String.Format("Failed to copy {0} to {1}", request.SourceKey, request.DestinationKey));
            }

            _S3Client.Delete(_bucketName, request.SourceKey, null);
        }
예제 #6
0
        /// <summary>
        /// This method calls the AWS SDK for .NET to copy an
        /// object from one Amazon S3 bucket to another.
        /// </summary>
        /// <param name="client">The Amazon S3 client object.</param>
        /// <param name="sourceKey">The name of the object to be copied.</param>
        /// <param name="destinationKey">The name under which to save the copy.</param>
        /// <param name="sourceBucketName">The name of the Amazon S3 bucket
        /// where the file is located now.</param>
        /// <param name="destinationBucketName">The name of the Amazon S3
        /// bucket where the copy should be saved.</param>
        /// <returns>Returns a CopyObjectResponse object with the results from
        /// the async call.</returns>
        public static async Task <CopyObjectResponse> CopyingObjectAsync(
            IAmazonS3 client,
            string sourceKey,
            string destinationKey,
            string sourceBucketName,
            string destinationBucketName)
        {
            var response = new CopyObjectResponse();

            try
            {
                var request = new CopyObjectRequest
                {
                    SourceBucket      = sourceBucketName,
                    SourceKey         = sourceKey,
                    DestinationBucket = destinationBucketName,
                    DestinationKey    = destinationKey,
                };
                response = await client.CopyObjectAsync(request);
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine($"Error copying object: '{ex.Message}'");
            }

            return(response);
        }
예제 #7
0
        public static bool rename_file(string oldFileName, string newFileName, bool isPublic)
        {
            try
            {
                AmazonS3Client client = get_client();
                if (client == null)
                {
                    return(false);
                }

                CopyObjectRequest request = new CopyObjectRequest();
                request.SourceBucket   = request.DestinationBucket = RaaiVanSettings.CephStorage.Bucket;
                request.SourceKey      = oldFileName;
                request.DestinationKey = newFileName;
                if (isPublic)
                {
                    request.CannedACL = S3CannedACL.PublicRead;
                }

                CopyObjectResponse response = client.CopyObject(request);
                bool result = response.HttpStatusCode == System.Net.HttpStatusCode.OK;

                if (result)
                {
                    delete_file(oldFileName);
                }

                return(result);
            }
            catch { return(false); }
        }
예제 #8
0
        private bool MoveToArchive(string fileName, string archiveLocation, string bucketName)
        {
            CopyObjectRequest copyRequest = new CopyObjectRequest()
            {
                SourceBucket      = bucketName,
                SourceKey         = fileName,
                DestinationBucket = bucketName,
                DestinationKey    = archiveLocation + fileName
            };

            CopyObjectResponse copyResponse = _client.CopyObject(copyRequest);

            if (copyResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                DeleteObjectRequest deleteRequest = new DeleteObjectRequest()
                {
                    BucketName = bucketName,
                    Key        = fileName
                };

                DeleteObjectResponse deleteResponse = _client.DeleteObject(deleteRequest);

                if (deleteResponse.HttpStatusCode == System.Net.HttpStatusCode.NoContent)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #9
0
        public async Task CopyObjectTest()
        {
            // ARRANGE
            CopyObjectRequest request = new CopyObjectRequest()
            {
                DestinationBucket = "mhaken",
                SourceBucket      = "mhaken-lambda",
                SourceKey         = "AWSAthenaUserMetrics/athena-metrics-636765132762278062.zip",
                DestinationKey    = "test/file.txt",
            };

            GetObjectMetadataRequest meta = new GetObjectMetadataRequest()
            {
                BucketName = request.SourceBucket,
                Key        = request.SourceKey
            };

            GetObjectMetadataResponse Meta = await client.GetObjectMetadataAsync(meta);

            // ACT
            CopyObjectResponse Response = await client.CopyOrMoveObjectAsync(request, 16777216, false);


            // ASSERT
            Assert.Equal(HttpStatusCode.OK, Response.HttpStatusCode);
            Assert.Equal(Meta.ETag, Response.ETag);
        }
예제 #10
0
        public bool CopyFile(string sourceBucket, string destinationBucket, string sourceFile, string destinationFile)
        {
            try
            {
                using (var client = new AmazonS3Client(AwsAccessKey, AwsSecretAccessKey, RegionEndpoint.USEast1))
                {
                    var request = new CopyObjectRequest
                    {
                        SourceBucket      = sourceBucket,
                        SourceKey         = sourceFile,
                        DestinationBucket = string.IsNullOrEmpty(destinationBucket) ? sourceBucket : destinationBucket,
                        DestinationKey    = destinationFile
                    };
                    var task = client.CopyObjectAsync(request);
                    task.Wait();
                    CopyObjectResponse response = task.Result;
                }

                return(true);
            }
            catch (AmazonS3Exception s3Exception)
            {
                throw s3Exception;
            }
            catch
            {
                throw;
            }
        }
예제 #11
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            ECSS3Client s3 = ECSS3Factory.getS3Client();

            // retrieve the key value from user to copy
            Console.Write("Enter the object key you want to copy: ");
            string key_source = Console.ReadLine();

            // retrieve the key value from user to name copied object
            Console.Write("Enter the object key for the copied object: ");
            string key_target = Console.ReadLine();

            // create the request object
            // When copying an object, you can preserve most of the metadata (default) or specify new metadata.
            // However, the ACL is not preserved and is set to private for the user making the request.
            CopyObjectRequest request = new CopyObjectRequest()
            {
                SourceBucket      = ECSS3Factory.S3_BUCKET,
                SourceKey         = key_source,
                DestinationBucket = ECSS3Factory.S3_BUCKET,
                DestinationKey    = key_target,
                MetadataDirective = S3MetadataDirective.COPY
            };

            // copy the object
            CopyObjectResponse response = s3.CopyObject(request);

            // print out object key/value for validation
            Console.WriteLine(string.Format("Copied object {0}/{1} to {2}/{3}", ECSS3Factory.S3_BUCKET, key_source, ECSS3Factory.S3_BUCKET, key_target));
            Console.ReadLine();
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, CopyObjectResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("ETag", targetDepth))
                    {
                        response.ETag = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("LastModified", targetDepth))
                    {
                        response.LastModified = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }


            IWebResponseData responseData = context.ResponseData;

            if (responseData.IsHeaderPresent("x-amz-expiration"))
            {
                response.Expiration = new Expiration(responseData.GetHeaderValue("x-amz-expiration"));
            }
            if (responseData.IsHeaderPresent("x-amz-copy-source-version-id"))
            {
                response.SourceVersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-copy-source-version-id"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption"))
            {
                response.ServerSideEncryptionMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }

            return;
        }
예제 #13
0
        /// <summary>
        /// Sets the storage class for the S3 Object's Version to the value
        /// specified.
        /// </summary>
        /// <param name="bucketName">The name of the bucket in which the key is stored</param>
        /// <param name="key">The key of the S3 Object whose storage class needs changing</param>
        /// <param name="version">The version of the S3 Object whose storage class needs changing</param>
        /// <param name="sClass">The new Storage Class for the object</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/>
        public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client)
        {
            if (sClass > S3StorageClass.ReducedRedundancy ||
                sClass < S3StorageClass.Standard)
            {
                throw new ArgumentException("Invalid value specified for storage class.");
            }

            if (null == s3Client)
            {
                throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests.");
            }

            // Get the existing ACL of the object
            GetACLRequest getACLRequest = new GetACLRequest();

            getACLRequest.BucketName = bucketName;
            getACLRequest.Key        = key;
            if (version != null)
            {
                getACLRequest.VersionId = version;
            }
            GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest);

            GetObjectMetadataResponse getMetadataResponse = s3Client.GetObjectMetadata(new GetObjectMetadataRequest()
                                                                                       .WithBucketName(bucketName)
                                                                                       .WithKey(key));


            // Set the storage class on the object
            CopyObjectRequest copyRequest = new CopyObjectRequest();

            copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName;
            copyRequest.SourceKey    = copyRequest.DestinationKey = key;
            copyRequest.ServerSideEncryptionMethod = getMetadataResponse.ServerSideEncryptionMethod;
            if (version != null)
            {
                copyRequest.SourceVersionId = version;
            }

            copyRequest.StorageClass = sClass;
            // The copyRequest's Metadata directive is COPY by default
            CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest);

            // Set the object's original ACL back onto it because a COPY
            // operation resets the ACL on the destination object.
            SetACLRequest setACLRequest = new SetACLRequest();

            setACLRequest.BucketName = bucketName;
            setACLRequest.Key        = key;
            if (version != null)
            {
                setACLRequest.VersionId = copyResponse.VersionId;
            }
            setACLRequest.ACL = getACLResponse.AccessControlList;
            s3Client.SetACL(setACLRequest);
        }
예제 #14
0
        /// <summary>
        /// Sets the server side encryption method for the S3 Object's Version to the value
        /// specified.
        /// </summary>
        /// <param name="bucketName">The name of the bucket in which the key is stored</param>
        /// <param name="key">The key of the S3 Object</param>
        /// <param name="version">The version of the S3 Object</param>
        /// <param name="method">The server side encryption method</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/>
        public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client)
        {
            if (null == s3Client)
            {
                throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests.");
            }

            // Get the existing ACL of the object
            GetACLRequest getACLRequest = new GetACLRequest();

            getACLRequest.BucketName = bucketName;
            getACLRequest.Key        = key;
            if (version != null)
            {
                getACLRequest.VersionId = version;
            }
            GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest);

            ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest()
                                                                          .WithBucketName(bucketName)
                                                                          .WithPrefix(key)
                                                                          .WithMaxKeys(1));

            if (listObjectResponse.S3Objects.Count != 1)
            {
                throw new ArgumentNullException("No object exists with this bucket name and key.");
            }

            // Set the storage class on the object
            CopyObjectRequest copyRequest = new CopyObjectRequest();

            copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName;
            copyRequest.SourceKey    = copyRequest.DestinationKey = key;
            copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy;
            if (version != null)
            {
                copyRequest.SourceVersionId = version;
            }

            copyRequest.ServerSideEncryptionMethod = method;
            // The copyRequest's Metadata directive is COPY by default
            CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest);

            // Set the object's original ACL back onto it because a COPY
            // operation resets the ACL on the destination object.
            SetACLRequest setACLRequest = new SetACLRequest();

            setACLRequest.BucketName = bucketName;
            setACLRequest.Key        = key;
            if (version != null)
            {
                setACLRequest.VersionId = copyResponse.VersionId;
            }
            setACLRequest.ACL = getACLResponse.AccessControlList;
            s3Client.SetACL(setACLRequest);
        }
예제 #15
0
파일: Function.cs 프로젝트: wolfems/AMSales
        static async Task ReadObjectDataAsync(Amazon.RegionEndpoint awsRegion, string bucketName, string keyName)
        {
            IAmazonS3 client = new AmazonS3Client(awsRegion);

            try
            {
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };
                using (GetObjectResponse response = await client.GetObjectAsync(request))
                    using (Stream responseStream = response.ResponseStream)
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            responseStream.CopyTo(memStream);
                            ParseExcelStream(memStream);

                            //using (StreamReader reader = new StreamReader(responseStream))
                            //{
                            //    string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
                            //    string contentType = response.Headers["Content-Type"];
                            //    Console.WriteLine("Object metadata, Title: {0}", title);
                            //    Console.WriteLine("Content type: {0}", contentType);

                            //    responseBody = reader.ReadToEnd(); // Now you process the response body.
                        }
                //processed/test.txt
                //S3FileInfo currentObject = new S3FileInfo(client, bucketName, keyName);
                CopyObjectRequest cpreq = new CopyObjectRequest
                {
                    SourceBucket      = bucketName,
                    SourceKey         = keyName,
                    DestinationBucket = bucketName,
                    DestinationKey    = "processed/" + keyName.Replace(".xlsx", ".processed")
                };
                CopyObjectResponse cpresp = await client.CopyObjectAsync(cpreq);

                var req2 = new DeleteObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };
                var resp2 = await client.DeleteObjectAsync(req2);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered ***. Message:'{0}' when reading an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when reading an object", e.Message);
            }
        }
예제 #16
0
 public void CopyFile(string sourceKey, string destinationKey)
 {
     CopyObjectRequest request = new CopyObjectRequest
     {
         SourceBucket      = CurrentBucketName,
         SourceKey         = sourceKey,
         DestinationBucket = CurrentBucketName,
         DestinationKey    = destinationKey,
         StorageClass      = Amazon.S3.S3StorageClass.Standard
     };
     CopyObjectResponse response = _transfer.S3Client.CopyObject(request);
 }
예제 #17
0
 public static void CopyFile(AmazonS3 s3Client, string sourcekey, string targetkey)
 {
     String            destinationPath = targetkey;
     CopyObjectRequest request         = new CopyObjectRequest()
     {
         SourceBucket      = BUCKET_NAME,
         SourceKey         = sourcekey,
         DestinationBucket = BUCKET_NAME,
         DestinationKey    = targetkey
     };
     CopyObjectResponse response = s3Client.CopyObject(request);
 }
예제 #18
0
파일: S3Service.cs 프로젝트: IDMS-IG/awslib
        public async Task CopyingObjectAsync(string objectKey, string destObjectKey)
        {
            CopyObjectRequest request = new CopyObjectRequest
            {
                SourceBucket      = bucketName,
                SourceKey         = objectKey,
                DestinationBucket = bucketName,
                DestinationKey    = destObjectKey
            };
            CopyObjectResponse response = await client.CopyObjectAsync(request);

            logger.Info($"Object {objectKey} was copid to {destObjectKey}.");
        }
예제 #19
0
        //------------------------------------------
        #endregion

        #region --------------CopyFile--------------
        public void CopyFile(string SourceBucket, String sourcePath, string DestinationBucket, String destinationPath)
        {
            // String destinationPath = "Els2/temp.txt";
            //SourceKey = "Els/" + "Demo Create File.txt",
            CopyObjectRequest request = new CopyObjectRequest()
            {
                SourceBucket      = SourceBucket,
                SourceKey         = sourcePath,
                DestinationBucket = DestinationBucket,
                DestinationKey    = destinationPath
            };
            CopyObjectResponse response = S3Client.CopyObject(request);
        }
예제 #20
0
 private void Publish(string stagingFolder, string fileName, string publishFolder, string bucketName)
 {
     fileName = System.IO.Path.GetFileName(fileName);
     CopyObjectRequest copyRequest = new CopyObjectRequest()
     {
         SourceBucket      = bucketName,
         SourceKey         = stagingFolder + "/" + fileName,
         DestinationBucket = bucketName,
         DestinationKey    = publishFolder + "/" + fileName
     };
     CopyObjectResponse result = _client.CopyObject(copyRequest);
     //TODO: inspect result.
 }
예제 #21
0
 public CopyResponse(CopyObjectResponse response,
                     string sourceBucket,
                     string sourceKey,
                     string destinationBucket,
                     string destinationKey)
 {
     this.Response          = response;
     this.SourceBucket      = sourceBucket;
     this.SourceKey         = sourceKey;
     this.DestinationBucket = destinationBucket;
     this.DestinationKey    = destinationKey;
     this.Exception         = null;
 }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            CopyObjectResponse copyObjectResponse = new CopyObjectResponse();

            while (context.Read())
            {
                if (context.get_IsStartElement())
                {
                    UnmarshallResult(context, copyObjectResponse);
                }
            }
            return(copyObjectResponse);
        }
예제 #23
0
        public async Task CopyObject()
        {
            //Upload an object to copy
            string sourceKey      = nameof(CopyObject);
            string destinationKey = sourceKey + "2";

            await UploadAsync(sourceKey).ConfigureAwait(false);

            CopyObjectResponse copyResp = await ObjectClient.CopyObjectAsync(BucketName, sourceKey, BucketName, destinationKey).ConfigureAwait(false);

            Assert.Equal(200, copyResp.StatusCode);

            await AssertAsync(destinationKey).ConfigureAwait(false);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, CopyObjectResponse response)
        {
            int currentDepth = context.get_CurrentDepth();
            int num          = currentDepth + 1;

            if (context.get_IsStartOfDocument())
            {
                num += 2;
            }
            while (context.Read())
            {
                if (context.get_IsStartElement() || context.get_IsAttribute())
                {
                    if (context.TestExpression("ETag", num))
                    {
                        response.ETag = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("LastModified", num))
                    {
                        response.LastModified = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                }
                else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth)
                {
                    return;
                }
            }
            IWebResponseData responseData = context.get_ResponseData();

            if (responseData.IsHeaderPresent("x-amz-expiration"))
            {
                response.Expiration = new Expiration(responseData.GetHeaderValue("x-amz-expiration"));
            }
            if (responseData.IsHeaderPresent("x-amz-copy-source-version-id"))
            {
                response.SourceVersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-copy-source-version-id"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption"))
            {
                response.ServerSideEncryptionMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-aws-kms-key-id"))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-aws-kms-key-id"));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }
        }
예제 #25
0
 public static void CopyFilesFromBucketToBucket(string sourceBucket, string sourceFileName, string destinationBucket, string destinationFileName)
 {
     using (AmazonS3Client client = new AmazonS3Client())
     {
         CopyObjectRequest request = new CopyObjectRequest()
         {
             SourceBucket      = sourceBucket,
             SourceKey         = sourceFileName,
             DestinationBucket = destinationBucket,
             DestinationKey    = destinationFileName
         };
         CopyObjectResponse response = client.CopyObject(request);
     }
 }
예제 #26
0
        public async Task <string> CopyImage(string sourceKey, string destinationKey)
        {
            CopyObjectRequest request = new CopyObjectRequest()
            {
                SourceBucket      = BUCKETNAME,
                SourceKey         = sourceKey,
                DestinationBucket = BUCKETNAME,
                DestinationKey    = destinationKey,
                CannedACL         = S3CannedACL.PublicReadWrite
            };
            CopyObjectResponse response = await _s3Client.CopyObjectAsync(request);

            return(string.Format("https://{0}.s3.amazonaws.com/{1}", BUCKETNAME, destinationKey));
        }
예제 #27
0
        public void RenameFile(string bucketName, string source, string target)
        {
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest()
            {
                SourceBucket      = bucketName,
                SourceKey         = source,
                DestinationBucket = bucketName,
                DestinationKey    = target
            };

            CopyObjectResponse copyObjectResponse = m_client.CopyObject(copyObjectRequest);

            DeleteObject(bucketName, source);
        }
예제 #28
0
        public void RenameFile(string bucketName, string source, string target)
        {
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest();

            copyObjectRequest.SourceBucket      = bucketName;
            copyObjectRequest.SourceKey         = source;
            copyObjectRequest.DestinationBucket = bucketName;
            copyObjectRequest.DestinationKey    = target;

            using (CopyObjectResponse copyObjectResponse = m_client.CopyObject(copyObjectRequest))
            { }

            DeleteObject(bucketName, source);
        }
예제 #29
0
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="S3Manager"></param>
        /// <param name="bucketName1"></param>
        /// <param name="bucketName2"></param>
        /// <param name="fileName1"></param>
        /// <param name="fileName2"></param>
        /// <returns></returns>
        public static ActionResult Move(IAmazonS3 S3Manager, string bucketName1, string fileName1, string bucketName2, string fileName2)
        {
            ActionResult ar = new ActionResult()
            {
                IsSuccess = false,
                Msg       = "Empty"
            };

            if (S3Manager != null)
            {
                ListObjectsRequest request = new ListObjectsRequest();
                request.BucketName = bucketName1;
                request.Prefix     = fileName1;

                ListObjectsResponse response = S3Manager.ListObjects(request);
                if (response.S3Objects.Count == 1)
                {
                    CopyObjectRequest copyObjectRequest = new CopyObjectRequest();
                    copyObjectRequest.SourceBucket      = bucketName1;
                    copyObjectRequest.SourceKey         = fileName1;
                    copyObjectRequest.DestinationBucket = bucketName2;
                    copyObjectRequest.DestinationKey    = fileName2;
                    CopyObjectResponse res = S3Manager.CopyObject(copyObjectRequest);
                    if (res.HttpStatusCode == HttpStatusCode.OK)
                    {
                        S3Manager.DeleteObject(bucketName1, fileName1);

                        ar.IsSuccess = true;
                        ar.Msg       = "移动成功!";
                    }
                    else
                    {
                        ar.IsSuccess = false;
                        ar.Msg       = "复制失败!";
                    }
                }
                else
                {
                    ar.IsSuccess = false;
                    ar.Msg       = "文件不存在!";
                }
            }



            return(ar);
        }
        ///// <summary>
        ///// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint.
        ///// </summary>
        ///// <param name="s3Object">The S3 Object</param>
        ///// <param name="websiteRedirectLocation">The redirect location</param>
        ///// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        //public static void SetWebsiteRedirectLocation(S3Object s3Object, string websiteRedirectLocation, IAmazonS3 s3Client)
        //{
        //    SetWebsiteRedirectLocation(s3Object.BucketName, s3Object.Key, websiteRedirectLocation, s3Client);
        //}

        /// <summary>
        /// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint.
        /// </summary>
        /// <param name="bucketName">The name of the bucket in which the key is stored</param>
        /// <param name="key">The key of the S3 Object</param>
        /// <param name="websiteRedirectLocation">The redirect location</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        public static void SetWebsiteRedirectLocation(IAmazonS3 s3Client, string bucketName, string key, string websiteRedirectLocation)
        {
            CopyObjectRequest copyRequest;
            PutACLRequest     putACLRequest;

            SetupForObjectModification(s3Client, bucketName, key, null, out copyRequest, out putACLRequest);

            copyRequest.WebsiteRedirectLocation = websiteRedirectLocation;
            CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest);

            if (!string.IsNullOrEmpty(copyResponse.SourceVersionId))
            {
                putACLRequest.VersionId = copyResponse.SourceVersionId;
            }

            s3Client.PutACL(putACLRequest);
        }