Load from Amazon S3 Storage
The following code snippet shows how to load a document from Amazon S3 Storage.
NoteRunnig this code requires installing AWSSDK.S3 NuGet package.
using System.IO;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
string key = "sample.docx";
Stream stream = await DownloadFileAsync(key);
using (Viewer viewer = new Viewer(stream))
{
    HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(viewOptions);
}
static async Task<Stream> DownloadFileAsync(string key)
{
    AmazonS3Client client = new AmazonS3Client();
    string bucketName = "my-bucket";
    GetObjectRequest request = new GetObjectRequest
    {
        Key = key,
        BucketName = bucketName
    };
    using (GetObjectResponse response = await client.GetObjectAsync(request))
    {
        MemoryStream stream = new MemoryStream();
        response.ResponseStream.CopyTo(stream);
        stream.Position = 0;
        return stream;
    }
}
Imports System.IO
Imports System.Threading.Tasks
Imports Amazon.S3
Imports Amazon.S3.Model
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...
Module Program
    Sub Main(args As String())
        Dim key As String = "sample.docx"
        Dim stream As Stream = DownloadFileAsync(key).GetAwaiter().GetResult()
        Using viewer As New Viewer(stream)
            Dim viewOptions As HtmlViewOptions = HtmlViewOptions.ForEmbeddedResources()
            viewer.View(viewOptions)
        End Using
    End Sub
    Private Async Function DownloadFileAsync(ByVal key As String) As Task(Of Stream)
        Dim client As New AmazonS3Client()
        Dim bucketName As String = "my-bucket"
        Dim request As New GetObjectRequest With {
                .Key = key,
                .BucketName = bucketName
                }
        Using response As GetObjectResponse = Await client.GetObjectAsync(request)
            Dim stream As New MemoryStream()
            response.ResponseStream.CopyTo(stream)
            stream.Position = 0
            Return stream
        End Using
    End Function
End Module
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.