当前位置: 首页>>代码示例>>C#>>正文


C# IAmazonS3.PutObjectAsync方法代码示例

本文整理汇总了C#中IAmazonS3.PutObjectAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IAmazonS3.PutObjectAsync方法的具体用法?C# IAmazonS3.PutObjectAsync怎么用?C# IAmazonS3.PutObjectAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IAmazonS3的用法示例。


在下文中一共展示了IAmazonS3.PutObjectAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UploadFile

        /// <summary>
        /// 上传本地文件到亚马逊S3服务器
        /// </summary>
        /// <param name="s3Client">S3的一个客户端对象</param>
        /// <param name="path">本地文件的路径</param>
        /// <param name="BucketName">要上传到S3的哪个桶</param>
        /// <param name="filePath">文件上传到S3后的路径,注意此路径会附加在BucketName后</param>
        /// <returns></returns>
        //public static PutObjectResponse UploadFile(IAmazonS3 s3Client, string path, string filePath, string BucketName = "ulp-learning")
        //{
        //    Stream inputStream = new FileStream(path, FileMode.Open, FileAccess.Read);

        //    // 默认添加public权限              
        //    PutObjectRequest putObjectRequest = new PutObjectRequest() { BucketName = BucketName, InputStream = inputStream, Key = filePath, AutoCloseStream = true, CannedACL = S3CannedACL.AuthenticatedRead };
        //    if (path.EndsWith(".3mv"))
        //    {
        //        putObjectRequest.ContentType = "audio/wav";
        //    }
        //    var result = s3Client.PutObject(putObjectRequest);
        //    foreach (var item in putObjectRequest.Headers.Keys)
        //    {
        //        Console.WriteLine(item + ":" + putObjectRequest.Headers[item]);
        //    }
        //    Console.WriteLine("ContentEncoding:" + putObjectRequest.Headers.ContentEncoding);

        //    return result;
        //}
        public static async void UploadFile(IAmazonS3 s3Client, string path, string filePath, string BucketName = "ulp-learning")
        {
            Stream inputStream = new FileStream(path, FileMode.Open, FileAccess.Read);

            // 默认添加public权限              
            PutObjectRequest putObjectRequest = new PutObjectRequest() { BucketName = BucketName, InputStream = inputStream, Key = filePath, AutoCloseStream = true, CannedACL = S3CannedACL.AuthenticatedRead };
            if (path.EndsWith(".3mv"))
            {
                putObjectRequest.ContentType = "audio/wav";
            }
            var result = await s3Client.PutObjectAsync(putObjectRequest);//一次尝试上传
            if (result == null || string.IsNullOrEmpty(result.ETag))
            {
                result = await s3Client.PutObjectAsync(putObjectRequest);//两次尝试上传
                if (result == null || string.IsNullOrEmpty(result.ETag))
                {
                    result = await s3Client.PutObjectAsync(putObjectRequest);//三次尝试上传

                }
            }
            foreach (var item in putObjectRequest.Headers.Keys)
            {
                Console.WriteLine(item + ":" + putObjectRequest.Headers[item]);
            }
            Console.WriteLine("ContentEncoding:" + putObjectRequest.Headers.ContentEncoding);

            return;
        }
开发者ID:LeehanLee,项目名称:L.W,代码行数:55,代码来源:Program.cs

示例2: CacheAlbum

        private async Task<Album> CacheAlbum(HttpClient httpClient, IAmazonS3 s3Client, JsonAlbum jsonAlbum, IList<string> cachedObjects, CancellationToken ctx)
        {
            var image = jsonAlbum.Image.Where(i => i.Size == JsonAlbumImageSize.Medium && i.Url != null).SingleOrDefault();
            if (image == null)
            {
                return null;
            }

            var album = new Album
            {
                Artist = jsonAlbum.Artist,
                Name = jsonAlbum.Name,
                Playcount = jsonAlbum.Playcount,
                Rank = jsonAlbum.Attributes.Rank,
                Url = jsonAlbum.Url
            };
            var objectKey = album.ToString().ToSlug();

            album.Thumbnail = new Uri($"https://s3-eu-west-1.amazonaws.com/{BucketName}/{objectKey}");

            // If the album art was cached already, return
            if (cachedObjects.Any(k => k == objectKey))
            {
                return album;
            }

            string mediaType;
            byte[] imageBytes;
            try
            {
                logger.LogInformation($"Downloading art for {album}");
                var response = await httpClient.GetAsync(image.Url, ctx);
                mediaType = response.Content.Headers?.ContentType?.MediaType;
                imageBytes = await response.Content.ReadAsByteArrayAsync();
            }
            catch (Exception ex)
            {
                // If anything happened here, log and continue. We don't want to retry.
                logger.LogWarning($"Hit exception whilst trying to download art for {album}", ex);
                return null;
            }

            PutObjectResponse putResponse;
            using (var ms = new MemoryStream(imageBytes))
            {
                logger.LogVerbose($"Storing art for {album}");
                putResponse = await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName = BucketName,
                    ContentType = mediaType,
                    InputStream = ms,
                    Key = objectKey,
                    StorageClass = S3StorageClass.ReducedRedundancy,
                    CannedACL = S3CannedACL.PublicRead
                }, ctx);
            }

            return album;
        }
开发者ID:alanedwardes,项目名称:aeblog,代码行数:59,代码来源:AlbumCacheTask.cs


注:本文中的IAmazonS3.PutObjectAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。