将生成的pdf上传到amazon S3

时间:2017-09-09 20:49:51

标签: c# amazon-s3

我使用itextsharp生成pdf。然后我想将该文件直接上传到amazon S3。

以前当我上传文件时,我可以使用它并获取该文件的输入流。像这样:

            AmazonS3Config S3Config = new AmazonS3Config
            {
                RegionEndpoint = RegionEndpoint.USEast1, //its default region set by amazon
            };

            AmazonS3Client client;
            using (client = new Amazon.S3.AmazonS3Client(_awsAccessKey, _awsSecretKey, S3Config))
            {
                var request = new PutObjectRequest()
                {
                    BucketName = _bucketName,
                    CannedACL = S3CannedACL.PublicRead,
                    Key = string.Format("UPLOADS/{0}", file.FileName),
                    InputStream = file.InputStream
                };

                client.PutObject(request);
            }

我还没有找到一种方法从我生成的pdf中获取输入流我已经读过了我可以重新创建该文件并将其读回来,但我无法让它工作。< / p>

编辑:添加用于生成pdf的代码

using (var ms = new System.IO.MemoryStream())
            {
                var document = new Document(PageSize.A4, 20f, 10f, 30f, 0f);
                {
                    PdfWriter writer = PdfWriter.GetInstance(document, ms);
                    document.Open();
                    try
                    {

                        Paragraph header = new Paragraph("Test Document");
                        document.Add(header);


                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} Exception caught.", ex);
                }
                finally
                {
                    document.Close();
                    byte[] bytes = ms.ToArray();
                    ms.Close();

                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Disposition", "attachment; filename=Contract for cancellation of " + order.OrderID + ".pdf");
                    Response.ContentType = "application/pdf";
                    Response.Buffer = true;
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite(bytes);
                    Response.End();
                }

非常感谢您对此有任何帮助

0 个答案:

没有答案
相关问题