Amazon.S3.IO.S3FileInfo.ExistsWithBucketCheck C# (CSharp) Method

ExistsWithBucketCheck() private method

private ExistsWithBucketCheck ( bool &bucketExists ) : bool
bucketExists bool
return bool
        internal bool ExistsWithBucketCheck(out bool bucketExists)
        {
            bucketExists = true;
            try
            {
                var request = new GetObjectMetadataRequest
                {
                    BucketName = bucket,
                    Key = S3Helper.EncodeKey(key)
                };
                request.BeforeRequestEvent += S3Helper.FileIORequestEventHandler;

                // If the object doesn't exist then a "NotFound" will be thrown
                s3Client.GetObjectMetadata(request);
                return true;
            }
            catch (AmazonS3Exception e)
            {
                if (string.Equals(e.ErrorCode, "NoSuchBucket"))
                {
                    bucketExists = false;
                    return false;
                }
                else if (string.Equals(e.ErrorCode, "NotFound"))
                {
                    return false;
                }
                throw;
            }
        }

Usage Example

Example #1
0
        internal S3FileStream(AmazonS3 s3Client, string bucket, string key, FileMode mode, FileAccess access, int buffersize)
        {
            file        = new S3FileInfo(s3Client, bucket, key);
            buffer      = new MemoryStream(buffersize);
            this.mode   = mode;
            this.access = access;

            fileExist = file.ExistsWithBucketCheck(out bucketExist);

            if ((access & FileAccess.Read) != FileAccess.Read)
            {
                canRead = false;
            }
            if ((access & FileAccess.Write) != FileAccess.Write)
            {
                canWrite = false;
            }

            switch (mode)
            {
            case FileMode.Append:
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Append requires Write access");
                }
                PopulateData();
                buffer.Seek(0, SeekOrigin.End);
                startPosition = buffer.Position;
                break;

            case FileMode.Create:
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Create requires Write access");
                }
                break;

            case FileMode.CreateNew:
                if (fileExist)
                {
                    throw new IOException("CreateNew requires the file not to already exist");
                }
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Create requires Write access");
                }
                break;

            case FileMode.Open:
                if (!fileExist)
                {
                    throw new IOException("Open requires the file to already exist");
                }
                PopulateData();
                break;

            case FileMode.OpenOrCreate:
                if (fileExist)
                {
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Create requires Write access");
                    }
                }
                break;

            case FileMode.Truncate:
                if (!fileExist)
                {
                    throw new IOException("Truncate requires the file to already exist");
                }
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Truncate requires Write access");
                }
                break;

            default:
                throw new ArgumentException("Invalid value", "mode");
            }
        }
All Usage Examples Of Amazon.S3.IO.S3FileInfo::ExistsWithBucketCheck