LitS3.S3Service.ObjectExists C# (CSharp) Method

ObjectExists() public method

Returns true if the given object exists in the given bucket.
public ObjectExists ( string bucketName, string key ) : bool
bucketName string
key string
return bool
        public bool ObjectExists(string bucketName, string key)
        {
            var request = new GetObjectRequest(this, bucketName, key, true);

            // This is the recommended method from the S3 API docs.
            try
            {
                using (GetObjectResponse response = request.GetResponse())
                    return true;
            }
            catch (WebException exception)
            {
                var response = exception.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    return false;
                else
                    throw;
            }
        }

Usage Example

Ejemplo n.º 1
0
        public ActionResult Delete(int photoItemID)
        {
            _mu = MembershipWrapper.GetUser();

            _pitm = new PhotoItem(photoItemID);

            if (_mu == null || _pitm.CreatedByUserID != Convert.ToInt32(_mu.ProviderUserKey))
                return RedirectToAction("Index");

            var s3 = new S3Service
            {
                AccessKeyID = AmazonCloudConfigs.AmazonAccessKey,
                SecretAccessKey = AmazonCloudConfigs.AmazonSecretKey
            };

            _pitm.Delete();

            if (string.IsNullOrEmpty(_pitm.FilePathStandard)) return RedirectToAction("Index");
            // delete the existing photos
            try
            {
                if (s3.ObjectExists(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathStandard))
                {
                    s3.DeleteObject(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathStandard);
                }

                if (s3.ObjectExists(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathRaw))
                {
                    s3.DeleteObject(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathRaw);
                }

                if (s3.ObjectExists(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathThumb))
                {
                    s3.DeleteObject(AmazonCloudConfigs.AmazonBucketName, _pitm.FilePathThumb);
                }
            }
            catch
            {
                // whatever
            }

            return RedirectToAction("Index");
        }
All Usage Examples Of LitS3.S3Service::ObjectExists