Exemplo n.º 1
0
		private void button1_Click(object sender, EventArgs e)
		{
			var dlg = new OpenFileDialog();
			dlg.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.gif";
			dlg.Multiselect = true;
			dlg.ShowDialog();

			if (dlg.FileName.Length <= 0)
				return;

			var oss = new OssClient(EndPoint, AccessKeyId, AccessKeySecret);

			foreach (var fileName in dlg.FileNames)
			{
				using (var file = new FileStream(fileName, FileMode.Open))
				{
					var meta = new ObjectMetadata();
					meta.ContentLength = file.Length;

					var destName = "avatar/" + Path.GetFileName(fileName);

					var result = oss.PutObject(BucketName, destName, file, meta);
					textBox1.AppendText(string.Format("Uploaded : {0}, {1}\n", destName, result.ETag));
				}
			}
		}
Exemplo n.º 2
0
        public static void UploadFile(string localServerPath,string folerPath,string fileName,string extType,byte[] file)
        {
            string localOrigineImageDirectory = localServerPath + folerPath;
            string localThumbDirectory = localServerPath + folerPath + "thumb/";
            if (!Directory.Exists(localOrigineImageDirectory))
                Directory.CreateDirectory(localOrigineImageDirectory);
            if (!Directory.Exists(localThumbDirectory))
                Directory.CreateDirectory(localThumbDirectory);
            
            // 原图存储在本地
            var fs = new FileStream(localOrigineImageDirectory + fileName + "." + extType, FileMode.Create, FileAccess.Write);
            fs.Write(file, 0, file.Length);
            fs.Flush();
            fs.Close();


            OssClient ossClient = new OssClient("http://oss-cn-beijing.aliyuncs.com", ConfigurationManager.AppSettings["ossId"], ConfigurationManager.AppSettings["ossKey"]);
            ObjectMetadata metadata=new ObjectMetadata();
            metadata.ContentType = extType=="jpg"?"image/jpeg":"";
            PutObjectResult result = ossClient.PutObject("baolinks",folerPath+fileName + "." + extType, new MemoryStream(file),metadata);
           

            //// 1000-751缩略图:立刻生成并写入本地
            //var oss1= ossClient.GetObject(new GetObjectRequest("baolinks", folerPath+fileName + "." + extType+"@!1000-751"));
            //WriteLocalFile(localThumbDirectory + fileName + "_1000_751" + "." + extType,StreamToBytes(oss1.Content));

            //// 350-215 缩略图:立刻生成并写入本地
            //var oss2 = ossClient.GetObject(new GetObjectRequest("baolinks", folerPath + fileName + "." + extType + "@!350-215"));
            //WriteLocalFile(localThumbDirectory + fileName + "_350_215" + "." + extType, StreamToBytes(oss2.Content));

            //// 90-90 缩略图:立刻生成并写入本地
            //var oss3 = ossClient.GetObject(new GetObjectRequest("baolinks", folerPath + fileName + "." + extType + "@!90-90"));
            //WriteLocalFile(localThumbDirectory + fileName + "_90_90" + "." + extType, StreamToBytes(oss3.Content));

        }
 public InitiateMultipartUploadRequest(string bucketName, string key, 
     ObjectMetadata objectMetadata)
 {
     BucketName = bucketName;
     Key = key;
     ObjectMetadata = objectMetadata;
 }
Exemplo n.º 4
0
 public static void UploadFile(string bucketName, string key, string fileName,string contentType)
 {
     using (FileStream fs = File.Open(fileName, FileMode.Open))
     {
         ObjectMetadata meta = new ObjectMetadata() { ContentType =contentType };
         GetOssClient().PutObject(bucketName, key, fs, meta);
     }
 }
Exemplo n.º 5
0
 public bool UploadStream(Stream fileStream, string savedName, string ContentType = defaultMime)
 {
     try
     {
         ObjectMetadata metadata = new ObjectMetadata();
         metadata.ContentType = ContentType;
         ossClient.PutObject(bucket, savedName, fileStream, metadata);
         return true;
     }
     catch
     {
         return false;
     }
 }
Exemplo n.º 6
0
 public bool UploadFile(string file, string fileName, string ContentType = defaultMime)
 {
     try
     {
         ObjectMetadata metadata = new ObjectMetadata();
         metadata.ContentType = ContentType;
         ossClient.PutObject(bucket, fileName, file, metadata);
         return true;
     }
     catch
     {
         return false;
     }
 }
Exemplo n.º 7
0
 public PutObjectResult PutObject(string bucketName, string key, System.IO.Stream content,
                                  Aliyun.OpenServices.OpenStorageService.ObjectMetadata metadata)
 {
     if (string.IsNullOrEmpty(bucketName))
     {
         throw new ArgumentNullException("bucketName can not be null or empty!");
     }
     if (string.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key can not be null or empty!");
     }
     if (content == null)
     {
         throw new ArgumentNullException("content can not be null!");
     }
     return(_ossClient.PutObject(bucketName, key, content, metadata));
 }
Exemplo n.º 8
0
        protected void btnPutObject_Click(object sender, EventArgs e)
        {
            OssClient ossClient = new OssClient(endpoint, accessId, accessKey);
            try
            {
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.ContentType = FileUpload1.PostedFile.ContentType;
                using (var fs = FileUpload1.PostedFile.InputStream)
                {
                    ossClient.PutObject(bucketName, DateTime.Now.ToString("yyyyMMddHHmmssfff"), fs, metadata);
                }
                Console.WriteLine("Bucket: " + bucketName);
            }
            catch (OssException ex)
            {
                // RequestID和HostID可以在有问题时用于联系客服诊断异常。
                Console.WriteLine(string.Format("创建失败。错误代码:{0}; 错误消息:{1}。\nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode,
                    ex.Message,
                    ex.RequestId,
                    ex.HostId));

            }
        }
Exemplo n.º 9
0
        public static string PutThumbnaiObject(Stream fs, string fileName)
        {


            OssClient client = OssClient;

            string bucketName = BucketName;
            string key = "thumbnail" + DateTime.Now.Date.ToString("yyyyMMdd") + "/" + fileName;
            //            const string fileToUpload = "<file to upload>";
            //
            //            try
            //            {
            // 1. put object to specified output stream
            //                using (var fs = File.Open(fileToUpload, FileMode.Open))
            //                {
            using (fs)
            {
//                Image img = Image.FromStream(fs);
                
                var metadata = new ObjectMetadata();
                metadata.CacheControl = "Private";
                metadata.ContentType = MimeMapping.GetMimeMapping(fileName);
                client.PutObject(bucketName, key, fs, metadata);

                // metadata = client.GetObjectMetadata(bucketName, key);
            }
            return key;
            //                }

            // 2. put object to specified file
            //client.PutObject(bucketName, key, fileToUpload);

            // 3. put object from specified object with multi-level virtual directory
            //key = "folder/sub_folder/key0";
            //client.PutObject(bucketName, key, fileToUpload);
            //            }
            //            catch (OssException ex)
            //            {
            //                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
            //                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            //            }
            //            catch (Exception ex)
            //            {
            //                Console.WriteLine("Failed with error info: {0}", ex.Message);
            //            }
        }
Exemplo n.º 10
0
 public static bool Upload(Stream stream, string name)
 {
     var metadata = new ObjectMetadata();
     metadata.UserMetadata.Add("name", name);
     metadata.CacheControl = "No-Cache";
     metadata.ContentLength = stream.Length;
     bool result = false;
     try
     {
         Debug.WriteLine(string.Format("/----- start upload:{0} to cloud-----/", name));
         using (stream)
         {
             var obj = ossClient.PutObject(bucket, name, stream, metadata);
             result = obj != null;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(string.Format("备份失败:{0}", ex.Message));
     }
     return result;
 }
Exemplo n.º 11
0
        private void button7_Click(object sender, EventArgs e)
        {
            var accessId = "CwGbCH4rghetfcHe";
            var accessKey = "IK97fqTq2t4jTxPOtYEzIBJXcZxs9F";
            var endpoint = "http://oss-cn-shenzhen.aliyuncs.com";

            var client = new OssClient(endpoint, accessId, accessKey);
      
            using (var fs = File.Open(@"E:\QMDownload\SoftMgr\f5362f59974a328a7811881df3b882a0.jpg", FileMode.Open))
            {
                var metadata = new ObjectMetadata();
                metadata.UserMetadata.Add("key0", "val0");
                metadata.ContentType = "image/jpeg";
                metadata.ContentLength = fs.Length;
                var result = client.PutObject("cdevspartan", "images/ddd/sm2.jpg", fs, metadata);
            }
        }
        public static string PutObject(Stream fsStream, string fileName)
        {
            var name = key + fileName;
            try
            {
                
                // 1. put object to specified output stream
                var metadata = new ObjectMetadata();
                metadata.CacheControl = "No-Cache";
                metadata.ContentLength = fsStream.Length;
                var result = client.PutObject(bucketName, name, fsStream, metadata);
                fsStream.Close();
                // 2. put object to specified file
                //client.PutObject(bucketName, key, fileToUpload);

                // 3. put object from specified object with multi-level virtual directory
                //key = "folder/sub_folder/key0";
                //client.PutObject(bucketName, key, fileToUpload);

            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
            return "http://"+bucketName+".oss-cn-qingdao.aliyuncs.com/"+name;
        }
        public static string AsyncPutObject(Stream fsStream, string fileName)
        {
            var name = key + fileName;
            try
            {
                
                // 1. put object to specified output stream
                var metadata = new ObjectMetadata();
                metadata.CacheControl = "No-Cache";
                metadata.ContentType = "text/html";
                client.BeginPutObject(bucketName, name, fsStream, metadata, PutObjectCallback, new string('a', 8));

                _evnet.WaitOne();
                fsStream.Close();
                
            }
            catch (OssException ex)
            {
            }
            catch (Exception ex)
            {
            }
            return "http://" + bucketName + ".oss-cn-qingdao.aliyuncs.com/" + name;
        }