Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
s3 with deflate or gzip =)
//install-package AWSSDK
class Program
{
private static IAmazonS3 _client;
private static byte[] DeflateCompress(Stream input)
{
using (var compressStream = new MemoryStream())
using (var compressor = new DeflateStream(compressStream, CompressionMode.Compress))
{
input.CopyTo(compressor);
compressor.Close();
return compressStream.ToArray();
}
}
private static byte[] GzipCompress(Stream input)
{
using (var compressStream = new MemoryStream())
using (var compressor = new GZipStream(compressStream, CompressionMode.Compress))
{
input.CopyTo(compressor);
compressor.Close();
return compressStream.ToArray();
}
}
private static Stream GzipDescompress(byte[] input)
{
var output = new MemoryStream();
using (var compressStream = new MemoryStream(input))
using (var decompressor = new GZipStream(compressStream, CompressionMode.Decompress))
decompressor.CopyTo(output);
output.Position = 0;
return output;
}
private static Stream DeflateDescompress(byte[] input)
{
var output = new MemoryStream();
using (var compressStream = new MemoryStream(input))
using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
decompressor.CopyTo(output);
output.Position = 0;
return output;
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
static void Main(string[] args)
{
const string accessKey = "";
const string secretKey = "";
var config = new AmazonS3Config { ServiceURL = "http://s3-sa-east-1.amazonaws.com" };
_client = AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, config);
DeflateSample();
ZipSample();
NormalFileSample();
Console.WriteLine("d");
Console.Read();
}
private static void NormalFileSample()
{
const string value =
"{\"NumeroCalculo\":\"0\",\"NumeroCotacaoMC\":\"49105574\",\"CodigoEstipulante\":\"0\",\"CodigoProduto\":\"10\",\"Segurado\":{\"NomeSegurado\":\"JOSUE DA SILVA\",\"CGC_CPF\":\"321.817.468-69\",\"TipoPessoa\":\"F\"},\"Item\":[{\"CodigoFabricante\":\"209\",\"TipoCombustivel\":\"F\",\"CodigoVeiculo\":\"16240\",\"AnoModelo\":\"2013\",\"AnoFabricacao\":\"2013\",\"ZeroKm\":\"N\",\"ValorVeiculo\":\"33820\",\"CEP\":\"05422-000\",\"DanosMateriais\":\"50000\",\"DanosCorporais\":\"50000\",\"DanosMorais\":\"0\",\"ValorApp\":\"0\",\"CodigoCobertura\":\"1\",\"CodigoFranquia\":\"1\",\"ClasseBonus\":\"0\",\"CodigoSeguradoraAnterior\":null,\"NumeroApoliceAnterior\":null,\"InicioVigencia\":\"26/06/2014\",\"FinalVigencia\":\"26/06/2015\",\"PercentualComissao\":\"20,00\",\"NumeroPassageiros\":\"5\",\"TipoVeiculo\":\"18026\",\"TipoDeFrota\":\"N\",\"PercentualAjuste\":\"100\",\"Blindagem\":\"N\",\"LmiBlindagem\":null,\"LmiKitGas\":null,\"TipoModalidade\":\"A\",\"TipoUsoVeiculo\":\"A\",\"NomeCondutor\":\"JOSUE DA SILVA\",\"DataNascimentoCondutor\":\"21/01/1983\",\"SexoCondutor\":\"M\",\"EstadoCivilCondutor\":\"B\",\"CNHCondutor\":null,\"CPFCondutor\":\"32181746869\",\"NumeroNegocioSubstituido\":\"\",\"Placa\":\"\",\"Chassi\":\"\",\"ChassiRemarcado\":\"N\",\"TipoSeguro\":\"1\",\"Desconto\":\"0\",\"Agravo\":\"0\",\"TipoAssistencia\":\"C\",\"Qbr\":[{\"CodigoPergunta\":\"249\",\"CodigoResposta\":\"659\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"250\",\"CodigoResposta\":\"662\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"252\",\"CodigoResposta\":\"672\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"255\",\"CodigoResposta\":\"685\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"256\",\"CodigoResposta\":\"690\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"257\",\"CodigoResposta\":\"696\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"258\",\"CodigoResposta\":\"701\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"259\",\"CodigoResposta\":\"707\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"260\",\"CodigoResposta\":\"711\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"261\",\"CodigoResposta\":\"714\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null}],\"Acessorios\":null,\"CoberturasAdicionais\":[{\"CodigoCobertura\":\"946\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"40\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"945\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"994\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"997\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"54\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"947\",\"CodigoOpcao\":\"2\"}]}]}";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(value));
var putRequest = new PutObjectRequest
{
CannedACL = S3CannedACL.Private,
BucketName = "minutologs",
InputStream = stream,
Key = "compressed.txt",
};
_client.PutObject(putRequest);
var getRequest = new GetObjectRequest
{
BucketName = "minutologs",
Key = "compressed.txt",
};
var response = _client.GetObject(getRequest);
var responseBytes = ReadFully(response.ResponseStream);
var responseStream = new MemoryStream(responseBytes);
using (var reader = new StreamReader(responseStream, Encoding.UTF8))
{
var originalText = reader.ReadToEnd();
Console.WriteLine(originalText);
}
}
private static void ZipSample()
{
const string value =
"{\"NumeroCalculo\":\"0\",\"NumeroCotacaoMC\":\"49105574\",\"CodigoEstipulante\":\"0\",\"CodigoProduto\":\"10\",\"Segurado\":{\"NomeSegurado\":\"JOSUE DA SILVA\",\"CGC_CPF\":\"321.817.468-69\",\"TipoPessoa\":\"F\"},\"Item\":[{\"CodigoFabricante\":\"209\",\"TipoCombustivel\":\"F\",\"CodigoVeiculo\":\"16240\",\"AnoModelo\":\"2013\",\"AnoFabricacao\":\"2013\",\"ZeroKm\":\"N\",\"ValorVeiculo\":\"33820\",\"CEP\":\"05422-000\",\"DanosMateriais\":\"50000\",\"DanosCorporais\":\"50000\",\"DanosMorais\":\"0\",\"ValorApp\":\"0\",\"CodigoCobertura\":\"1\",\"CodigoFranquia\":\"1\",\"ClasseBonus\":\"0\",\"CodigoSeguradoraAnterior\":null,\"NumeroApoliceAnterior\":null,\"InicioVigencia\":\"26/06/2014\",\"FinalVigencia\":\"26/06/2015\",\"PercentualComissao\":\"20,00\",\"NumeroPassageiros\":\"5\",\"TipoVeiculo\":\"18026\",\"TipoDeFrota\":\"N\",\"PercentualAjuste\":\"100\",\"Blindagem\":\"N\",\"LmiBlindagem\":null,\"LmiKitGas\":null,\"TipoModalidade\":\"A\",\"TipoUsoVeiculo\":\"A\",\"NomeCondutor\":\"JOSUE DA SILVA\",\"DataNascimentoCondutor\":\"21/01/1983\",\"SexoCondutor\":\"M\",\"EstadoCivilCondutor\":\"B\",\"CNHCondutor\":null,\"CPFCondutor\":\"32181746869\",\"NumeroNegocioSubstituido\":\"\",\"Placa\":\"\",\"Chassi\":\"\",\"ChassiRemarcado\":\"N\",\"TipoSeguro\":\"1\",\"Desconto\":\"0\",\"Agravo\":\"0\",\"TipoAssistencia\":\"C\",\"Qbr\":[{\"CodigoPergunta\":\"249\",\"CodigoResposta\":\"659\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"250\",\"CodigoResposta\":\"662\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"252\",\"CodigoResposta\":\"672\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"255\",\"CodigoResposta\":\"685\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"256\",\"CodigoResposta\":\"690\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"257\",\"CodigoResposta\":\"696\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"258\",\"CodigoResposta\":\"701\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"259\",\"CodigoResposta\":\"707\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"260\",\"CodigoResposta\":\"711\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"261\",\"CodigoResposta\":\"714\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null}],\"Acessorios\":null,\"CoberturasAdicionais\":[{\"CodigoCobertura\":\"946\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"40\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"945\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"994\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"997\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"54\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"947\",\"CodigoOpcao\":\"2\"}]}]}";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value)))
{
var compressedStream = new MemoryStream(GzipCompress(stream));
var putRequest = new PutObjectRequest
{
CannedACL = S3CannedACL.Private,
BucketName = "minutologs",
InputStream = compressedStream,
Key = "compressed.gz",
};
_client.PutObject(putRequest);
}
var getRequest = new GetObjectRequest
{
BucketName = "minutologs",
Key = "compressed.gz",
};
var response = _client.GetObject(getRequest);
var responseBytes = ReadFully(response.ResponseStream);
var decompressedStream = GzipDescompress(responseBytes);
decompressedStream.Position = 0;
using (var reader = new StreamReader(decompressedStream, Encoding.UTF8))
{
var originalText = reader.ReadToEnd();
Console.WriteLine(originalText);
}
}
private static void DeflateSample()
{
const string value =
"{\"NumeroCalculo\":\"0\",\"NumeroCotacaoMC\":\"49105574\",\"CodigoEstipulante\":\"0\",\"CodigoProduto\":\"10\",\"Segurado\":{\"NomeSegurado\":\"JOSUE DA SILVA\",\"CGC_CPF\":\"321.817.468-69\",\"TipoPessoa\":\"F\"},\"Item\":[{\"CodigoFabricante\":\"209\",\"TipoCombustivel\":\"F\",\"CodigoVeiculo\":\"16240\",\"AnoModelo\":\"2013\",\"AnoFabricacao\":\"2013\",\"ZeroKm\":\"N\",\"ValorVeiculo\":\"33820\",\"CEP\":\"05422-000\",\"DanosMateriais\":\"50000\",\"DanosCorporais\":\"50000\",\"DanosMorais\":\"0\",\"ValorApp\":\"0\",\"CodigoCobertura\":\"1\",\"CodigoFranquia\":\"1\",\"ClasseBonus\":\"0\",\"CodigoSeguradoraAnterior\":null,\"NumeroApoliceAnterior\":null,\"InicioVigencia\":\"26/06/2014\",\"FinalVigencia\":\"26/06/2015\",\"PercentualComissao\":\"20,00\",\"NumeroPassageiros\":\"5\",\"TipoVeiculo\":\"18026\",\"TipoDeFrota\":\"N\",\"PercentualAjuste\":\"100\",\"Blindagem\":\"N\",\"LmiBlindagem\":null,\"LmiKitGas\":null,\"TipoModalidade\":\"A\",\"TipoUsoVeiculo\":\"A\",\"NomeCondutor\":\"JOSUE DA SILVA\",\"DataNascimentoCondutor\":\"21/01/1983\",\"SexoCondutor\":\"M\",\"EstadoCivilCondutor\":\"B\",\"CNHCondutor\":null,\"CPFCondutor\":\"32181746869\",\"NumeroNegocioSubstituido\":\"\",\"Placa\":\"\",\"Chassi\":\"\",\"ChassiRemarcado\":\"N\",\"TipoSeguro\":\"1\",\"Desconto\":\"0\",\"Agravo\":\"0\",\"TipoAssistencia\":\"C\",\"Qbr\":[{\"CodigoPergunta\":\"249\",\"CodigoResposta\":\"659\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"250\",\"CodigoResposta\":\"662\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"252\",\"CodigoResposta\":\"672\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"255\",\"CodigoResposta\":\"685\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"256\",\"CodigoResposta\":\"690\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"257\",\"CodigoResposta\":\"696\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"258\",\"CodigoResposta\":\"701\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"259\",\"CodigoResposta\":\"707\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"260\",\"CodigoResposta\":\"711\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null},{\"CodigoPergunta\":\"261\",\"CodigoResposta\":\"714\",\"CodigoSubResposta\":null,\"CodigoSubResposta2\":null}],\"Acessorios\":null,\"CoberturasAdicionais\":[{\"CodigoCobertura\":\"946\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"40\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"945\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"994\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"997\",\"CodigoOpcao\":\"2\"},{\"CodigoCobertura\":\"54\",\"CodigoOpcao\":\"1\"},{\"CodigoCobertura\":\"947\",\"CodigoOpcao\":\"2\"}]}]}";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value)))
{
var compressedStream = new MemoryStream(DeflateCompress(stream));
var putRequest = new PutObjectRequest
{
CannedACL = S3CannedACL.Private,
BucketName = "minutologs",
InputStream = compressedStream,
Key = "compressed",
};
_client.PutObject(putRequest);
}
var getRequest = new GetObjectRequest
{
BucketName = "minutologs",
Key = "compressed",
};
var response = _client.GetObject(getRequest);
var responseBytes = ReadFully(response.ResponseStream);
var decompressedStream = DeflateDescompress(responseBytes);
decompressedStream.Position = 0;
using (var reader = new StreamReader(decompressedStream, Encoding.UTF8))
{
var originalText = reader.ReadToEnd();
Console.WriteLine(originalText);
}
}
}
@lfreneda

This comment has been minimized.

Copy link
Owner Author

@lfreneda lfreneda commented Jun 30, 2014

install-package AWSSDK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.