I am currently using the following code to extract and extract string data from Amazon C #:
GetObjectRequest getObjectRequest = new GetObjectRequest().WithBucketName(bucketName).WithKey(key); using (S3Response getObjectResponse = client.GetObject(getObjectRequest)) { using (Stream s = getObjectResponse.ResponseStream) { using (GZipStream gzipStream = new GZipStream(s, CompressionMode.Decompress)) { StreamReader Reader = new StreamReader(gzipStream, Encoding.Default); string Html = Reader.ReadToEnd(); parseFile(Html); } } }
I want to cancel this code so that I can compress and unload string data on S3 without writing to disk. I tried the following but get an exception:
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKeyID, AWSSecretAccessKeyID)) { string awsPath = AWSS3PrefixPath + "/" + keyName+ ".htm.gz"; byte[] buffer = Encoding.UTF8.GetBytes(content); using (MemoryStream ms = new MemoryStream()) { using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress)) { zip.Write(buffer, 0, buffer.Length); PutObjectRequest request = new PutObjectRequest(); request.InputStream = ms; request.Key = awsPath; request.BucketName = AWSS3BuckenName; using (S3Response putResponse = client.PutObject(request)) {
The exception that I get is:
Unable to access closed stream.
What am I doing wrong?
EDIT:
An exception occurs on the closing bracket using (GZipStream zip
Stack trace:
in System.IO.MemoryStream.Write (byte [] buffer, Int32 offset, Int32 count)
at System.IO.Compression.DeflateStream.Dispose (Boolean disposition) at System.IO.Stream.Close () at System.IO.Compression.GZipStream.Dispose (Boolean disposition) at System.IO.Stream.Close ()
c # amazon-s3 amazon-web-services
BigJoe714
source share