C# 调用 百度云 BOS 服务

1.打开百度智能云 https://console.bce.baidu.com/ 注册登录
2.鼠标右上角头像图标弹出选择 “安全认证”获取 访问密钥ID(Access Key Id, 后文简称AK)和秘密访问密钥(Secret Access Key, 后文简称SK)
3.打开BOS服务,开通服务,进入到创建Buckets界面,发现要收费,没有继续了。代码只有一部分可以运行,仅供参考
4.打开VS,新建项目, Nuget搜索BceSdkDotNet,应用到项目,代码如下

class Program
    {
        public static void FileDownLoad()
        {
            var client = GenerateBosClient();
            var bucketName = "/Program";    //指定Bucket名称(文件夹名称)
            var objectKey = "分布式数据库结构图.fw.png";     //指定object名称(文件名字)// 获取Object
            var bosObject = client.GetObject(bucketName, objectKey);
            var meta = bosObject.ObjectMetadata;
            var objectContent = bosObject.ObjectContent;
            var fileStream = new FileInfo(objectKey).OpenWrite();      //指定下载文件的目录/文件名
            var buffer = new byte[2048];
            int count = 0;
            while ((count = objectContent.Read(buffer, 0, buffer.Length)) > 0)
            {
                fileStream.Write(buffer, 0, count);
            }

            // 关闭流
            objectContent.Close();
            fileStream.Close();
        }

        private static BosClient GenerateBosClient()
        {
            const string accessKeyId = ""; // 您的Access Key ID
            const string secretAccessKey = ""; // 您的Secret Access Key
            const string endpoint = "http://bj.bcebos.com";        //指定Bucket所在区域域名

            // 初始化一个BosClient
            BceClientConfiguration config = new BceClientConfiguration();
            config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
            config.Endpoint = endpoint;

            return new BosClient(config);
        }

        public static void GetBuckets()
        {
            BosClient client = GenerateBosClient();
            List<BucketSummary> buckets = client.ListBuckets().Buckets;
            foreach (BucketSummary bucket in buckets)
            {
                Console.WriteLine(bucket.Name);
            }
        }

        static void Main(string[] args)
        {
            GetBuckets();
            FileDownLoad();
        }
    }

 

C# 调用 百度云 BOS 服务

上一篇:QtCreator 控件自适应窗体的方法


下一篇:C#短链接生成方法