Пример #1
0
        public ErrorTypes GetFileInfo(string strPath, out StorageFileInfo oStorageFileInfo)
        {
            oStorageFileInfo = null;
            ErrorTypes eError = ErrorTypes.StorageGetInfo;

            try
            {
                string strFileKey = GetFilePath(strPath);
                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    Amazon.S3.Model.GetObjectMetadataRequest oRequest = new Amazon.S3.Model.GetObjectMetadataRequest()
                                                                        .WithBucketName(m_strBucketName).WithKey(strFileKey);

                    using (Amazon.S3.Model.GetObjectMetadataResponse oResponse = oS3Client.GetObjectMetadata(oRequest))
                    {
                        oStorageFileInfo = new StorageFileInfo(oResponse.ContentLength, oResponse.LastModified);
                        eError           = ErrorTypes.NoError;
                    }
                }
            }
            catch
            {
            }
            return(eError);
        }
Пример #2
0
        /// <summary>
        /// Gets meta data of a file on S3.
        /// It throws AmazonS3Exception if any error happens
        /// </summary>
        /// <param name="keyName">key name</param>
        /// <returns></returns>
        private Amazon.S3.Model.GetObjectMetadataResponse S3GetObjectMetaData(string keyName)
        {
            var request = new Amazon.S3.Model.GetObjectMetadataRequest();

            request.BucketName = s3Settings.BucketName;
            request.Key        = keyName;
            var objectMetaData = s3Client.GetObjectMetadata(request);

            return(objectMetaData);
        }
Пример #3
0
 /// <summary>
 /// Gets information and metadata for an object on
 /// S3 without needing to download it.
 /// </summary>
 /// <param name="bucket">The bucket that the object is in.</param>
 /// <param name="key">The key location for the object.</param>
 /// <returns></returns>
 public static Amazon.S3.Model.GetObjectMetadataResponse GetObjectMetadataResponse(string bucket, string key)
 {
     Amazon.S3.Model.GetObjectMetadataResponse response = new Amazon.S3.Model.GetObjectMetadataResponse();
     using (Amazon.S3.IAmazonS3 client = new Factory().S3Client())
     {
         Amazon.S3.Model.GetObjectMetadataRequest request = new Amazon.S3.Model.GetObjectMetadataRequest()
         {
             BucketName = bucket,
             Key = key
         };
         response = client.GetObjectMetadata(request);
     }
     return response;
 }
        public long GetSize()
        {
            if (!_size.HasValue)
            {
                var request =
                    new Amazon.S3.Model.GetObjectMetadataRequest
                {
                    BucketName = _bucketName,
                    Key        = _keyName
                };

                _size = _amazonS3.GetObjectMetadataAsync(request).Result.ContentLength;
            }

            return(_size.Value);
        }
Пример #5
0
        public async Task TryDeleteFile(string filePath, CancellationToken cancellationToken)
        {
            var client = GetClient();

            try
            {
                var request = new Amazon.S3.Model.GetObjectMetadataRequest();
                request.BucketName = _bucketName;
                request.Key        = filePath;
                await client.GetObjectMetadataAsync(request, cancellationToken);
                await DeleteFile(filePath, cancellationToken);
            }
            catch
            {
            }
        }
Пример #6
0
        public ErrorTypes GetFileInfo(string strPath, out StorageFileInfo oStorageFileInfo)
        {
            oStorageFileInfo = null;
            ErrorTypes eError = ErrorTypes.StorageGetInfo;

            try
            {
                string strFileKey = GetFilePath(strPath);
                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    Amazon.S3.Model.GetObjectMetadataRequest oRequest = new Amazon.S3.Model.GetObjectMetadataRequest()
                        .WithBucketName(m_strBucketName).WithKey(strFileKey);

                    using(Amazon.S3.Model.GetObjectMetadataResponse oResponse = oS3Client.GetObjectMetadata(oRequest))
                    {
                        oStorageFileInfo = new StorageFileInfo(oResponse.ContentLength, oResponse.LastModified);
                        eError = ErrorTypes.NoError;
                    }
                }
            }
            catch
            {
            }
            return eError;
        }
        public void DeployPush(string appName, string s3Location, string sourcePath)
        {
            var s3 = new S3LocationParser(s3Location);

            using (var s3Client = new Amazon.S3.AmazonS3Client(Credentials, Amazon.RegionEndpoint.USEast1))
            {
                var getRequest = new Amazon.S3.Model.GetObjectMetadataRequest
                {
                    BucketName = s3.BucketName,
                    Key = s3.Key
                };

                try
                {
                    var getResponse = s3Client.GetObjectMetadata(getRequest);

                    // If we got this far, the file already exists in the S3 bucket, so get out!
                    throw new S3KeyAlreadyExistsException(s3);
                }
                catch (AmazonS3Exception ex)
                {
                    // If we got this far, then it's because the S3 file doesn't exist.
                    // Therefore, it's OK to upload with this key.
                    var zipFileName = s3.FileName;
                    var g = Guid.NewGuid().ToString();
                    var outputFile = Path.Combine(Path.GetTempPath(), g + ".tmp");
                    ZipFile.CreateFromDirectory(sourcePath, outputFile);
                    var zipFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), zipFileName);

                    if (File.Exists(zipFile))
                        File.Delete(zipFile);

                    File.Move(outputFile, zipFile);

                    var putRequest = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName = s3.BucketName,
                        FilePath = zipFile,
                        Key = s3.Key
                    };

                    var putResponse = s3Client.PutObject(putRequest);

                    using (var cdClient = new Amazon.CodeDeploy.AmazonCodeDeployClient(Credentials, Amazon.RegionEndpoint.USEast1))
                    {
                        var revLocation = new Amazon.CodeDeploy.Model.RevisionLocation
                        {
                            RevisionType = Amazon.CodeDeploy.RevisionLocationType.S3,
                            S3Location = new Amazon.CodeDeploy.Model.S3Location
                                            {
                                                Bucket = s3.BucketName,
                                                BundleType = Amazon.CodeDeploy.BundleType.Zip,
                                                Key = s3.Key
                                            }
                        };

                        var regRequest = new Amazon.CodeDeploy.Model.RegisterApplicationRevisionRequest
                        {
                            ApplicationName = appName,
                            Revision = revLocation
                        };

                        var regResponse = cdClient.RegisterApplicationRevision(regRequest);

                        var x = 0;
                    }
                }
            }
        }