阿里云OSS多图上传

 

代码

        OssClient ossClient;
        String bucketName = "hd3p";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=UTF-8";
            HttpPostedFile imgFile = context.Request.Files["Filedata"];
            if (imgFile != null)
            {
                string accessid = "ut5hhgas69gKf3jT";          // AccessID
                string accesskey = "Rv4crh7unAKz8SJbZ3TffNwdLsblze";     // AccessKey
                ossClient = new Aliyun.OpenServices.OpenStorageService.OssClient(accessid, accesskey); //当然这里可以封装下
                ObjectMetadata meta = new ObjectMetadata();
                meta.ContentType = "image/jpeg";
                string key = "pic/" + imgFile.FileName;
                PutObjectResult result = ossClient.PutObject(bucketName, key, imgFile.InputStream, meta);//上传图片
                AccessControlList accs = ossClient.GetBucketAcl(bucketName);
                string imgurl = string.Empty;
                if (!accs.Grants.Any())//判断是否有读取权限
                {
                    imgurl = ossClient.GeneratePresignedUri(bucketName, key, DateTime.Now.AddMinutes(5)).AbsoluteUri; //生成一个签名的Uri 有效期5分钟
                }
                else
                {

                    imgurl = string.Format("http://{0}.oss.aliyuncs.com/{1}", bucketName, key);
                } 
                context.Response.Write(String.Format("{0}|{1}|{2}|{3}", imgurl, imgurl, imgurl, key));//
            }
        }

 源码

posted @ 2013-05-27 11:50  王大湿  阅读(21636)  评论(2编辑  收藏  举报