C#阿里云上传和下载示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aliyun.OpenServices.OpenStorageService;
using System.IO;
using System.Collections;

namespace AliyunTest
{
    class Program
    {

        static void Main(string[] args)
        {
            string accessId = "NmBlLQLtoPjNy1fV";
            string accessKey = "Ofp39rtQZPgzlxPGgtSE9MTWtPTXvW";
            string bucketName = "haolipeng";
            //key是上传到阿里服务器以后的文件名称
            String key = "Test.mp3";

            //String uploadFilePath = "D:/MyFile/mp3/爱笑的眼睛.mp3";
            String uploadFilePath = "D:/MyFile/VBR.mp3";
            String downloadFilePath = "D:/Test.mp3";

            OssClient client = new OssClient(accessId, accessKey);

            //自己创建Bucket
            ensureBucket(client, bucketName);

            try
            {
                client.SetBucketAcl(bucketName, CannedAccessControlList.PublicReadWrite);

                //Console.WriteLine("正在上传...");
                //uploadFile(client, bucketName, key, uploadFilePath);

                Console.WriteLine("正在下载...");
                downloadFile(client, bucketName, key, downloadFilePath);
            }
            finally
            {
                //deleteBucket(client, bucketName);
                Console.WriteLine("上传成功!");
            }
        }
        //ensureBucket函数实现
        private static void ensureBucket(OssClient client, String bucketName)
        {
            try
            {
                client.CreateBucket(bucketName);
                Console.WriteLine("创建成功!Bucket: " + bucketName);
            }
            catch (OssException ex)
            {
                if (ex.ErrorCode == OssErrorCode.BucketAlreadyExists)
                {
                    // 这里示例处理一种特定的ErrorCode。
                    Console.WriteLine(string.Format("Bucket '{0}' 已经存在,请更改名称后再创建。", bucketName));
                }
                else
                {
                    // RequestID和HostID可以在有问题时用于联系客服诊断异常。
                    Console.WriteLine(string.Format("创建失败。错误代码:{0}; 错误消息:{1}。\nRequestID:{2}\tHostID:{3}",ex.ErrorCode,
                                                                        ex.Message,ex.RequestId,ex.HostId));
                }
            }
        }

        // 删除一个Bucket和其中的Objects
        private static void deleteBucket(OssClient client, String bucketName)
        {
            ObjectListing ol = client.ListObjects(bucketName);

            List<OssObjectSummary> listDeletes = new List<OssObjectSummary>(ol.ObjectSummaries);
            //listDeletes.AddRange(ol.ObjectSummaries);先构造后添加也可以

            foreach (OssObjectSummary s in listDeletes)
            {
                //遍历每个Bucket下ObjectName
                String ObjectName = s.Key;
                //删除指定bucket下的Bucket下的ObjectName
                client.DeleteObject(bucketName, ObjectName);
            }
            //删除bucket
            client.DeleteBucket(bucketName);
        }

        // 上传文件
        private static void uploadFile(OssClient client, String bucketName, String key, String filename)
        {
            FileStream ReadStream = File.Open(filename,FileMode.Open);

            ObjectMetadata objectMeta = new ObjectMetadata();
            //设置内容长度
            objectMeta.ContentLength = ReadStream.Length;

            //设置内容类型
            objectMeta.ContentType = "image/jpeg";

            client.PutObject(bucketName, key, ReadStream, objectMeta);
        }

         //下载文件(测试下块下载)
        private static void downloadFile(OssClient client, String bucketName, String key, String filename)
        {
            GetObjectRequest downloadRequest = new GetObjectRequest(bucketName, key);
            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);

            downloadRequest.SetRange(1000, 5000);
            client.GetObject(downloadRequest, fs);
        }
    }
}

你可能感兴趣的:(C#阿里云上传和下载示例代码)