Exemplo n.º 1
0
        static async Task WritingAnObject(S3SettingsBo settings, S3FileBo file)
        {
            try
            {
                PutObjectRequest request = new PutObjectRequest()
                {
                    //ContentBody = System.Text.Encoding.UTF8.GetString(file.Content),
                    ContentType = ContentTypeHelper.GetMimeType(file.Name),
                    InputStream = new MemoryStream(file.Content),
                    BucketName  = settings.Bucket,
                    Key         = file.Link.Replace($"{settings.Address}/", ""),
                    CannedACL   = S3CannedACL.PublicRead,
                    ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256
                };
                System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = file.Name,
                    Inline   = ContentTypeHelper.FileIsInline(file.Name) // false = prompt the user for downloading;  true = browser to try to show the file inline
                };
                request.Headers.ContentDisposition = cd.ToString();
                request.Headers.CacheControl       = "max-age=31536000";
                var result = await client.PutObjectAsync(request);

                if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception($"An error occurred with the code '{result.HttpStatusCode}' when writing an object");
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    throw new Exception("Please check the provided AWS Credentials.");
                }
                else
                {
                    throw new Exception($"An error occurred with the message '{amazonS3Exception.Message}' when writing an object");
                }
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Get(string fileKey)
        {
            // cases:
            // return file content
            // redirect to s3
            // redirect to main file service
            // file not found
            try
            {
                var file = await this._fileAppService.Get(fileKey);

                if (file.StorageType == StorageType.S3)
                {
                    return(new RedirectResult(file.Link));
                }
                System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = HttpUtility.UrlEncode(file.Name, Encoding.UTF8).Replace("+", " ").Replace("%2b", "+"),
                    Inline   = ContentTypeHelper.FileIsInline(file.Name) // false = prompt the user for downloading;  true = browser to try to show the file inline
                };
                Response.Headers.Add("Content-Disposition", cd.ToString());
                Response.Headers.Add("X-Content-Type-Options", "nosniff");
                Response.Headers.Add("cache-control", "max-age=31536000");
                return(File(file.Content, ContentTypeHelper.GetMimeType(file.Name), DateTimeOffset.Now.AddSeconds(1), new EntityTagHeaderValue(new Microsoft.Extensions.Primitives.StringSegment("\"inline\""))));
            }
            catch (FileKeyNotFoundException ex)
            {
                if (await this._configService.GetBool("IsMainServer"))
                {
                    throw ex;
                }
                else
                {
                    var url = await this._configService.Get("MainServiceUrl");

                    return(new RedirectResult($"{url}/{fileKey}"));
                    // redirect to main file service
                }
            }
        }