[SDK2.2]Windows Azure Storage (15) 使用WCF服务,将本地图片上传至Azure Storage (上) 服务器端代码

简介:

Windows Azure Platform 系列文章目录

 

  这几天工作上的内容,把项目文件和源代码拿出来给大家分享下。

  源代码下载:Part1    Part2    Part3

 

  我们在写WEB服务的时候,经常需要把本地的文件上传至服务器端进行保存,类似于上传附件的功能。

  在本章中,我们将新建WCF服务并上传至Azure云端,实现把本地的图片通过WCF保存至Azure Storage。

  本章需要掌握的技术点:

  1.WCF服务

  2.Azure Storage

  本章将首先介绍服务器端程序代码。

 

  1.首先我们用管理员身份,运行VS2013。

  2.新建Windows Azure Cloud Project,并命名为LeiAzureService

  

  3.添加"WCF Service Web Role",并重命名为LeiWCFService。

  

  4.打开IE浏览器,登录http://manage.windowsazure.com,在Storage栏,新建storage name为leiwcfstorage

  

 

 

  5.我们回到VS2013,选择Project LeiAzureService,展开Roles->LeiWCFService,右键属性

  

   6.在弹出的窗口里,Configuration栏,将Instance count设置成2, VM Size选择Small

  

  这样,就同时有2台 small size(1core/1.75GB)的Cloud Service自动做负载均衡了。

 

  7.在Settings栏,选择Add Setting,设置Name为StorageConnectionString,Type选择Connection String,然后点击Value栏的按钮

  在Create Storage Connection String中,选择Account Name为我们在步骤4中创建的leiwcfstorage

  

 

  8.再次点击Add Setting,设置Name为ContainerName,Type选择String,Value设置为photos。切记Value的值必须是小写

  

 

  9.在Project LeiWCFService中,修改IService1.cs

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace LeiWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/UploadPic", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        string UploadPic(Stream ImageStream);
       
    }
}
复制代码

  

  10.修改Service1.svc

  这个类的主要功能是读取Azure配置文件cscfg的节点信息,并根据storage connection string,创建Container,并将本地的图片文件名重命名为GUID,并保存至Container

  项目中需要添加相关的引用,笔者就不详述了。

复制代码
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;


namespace LeiWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public string UploadPic(Stream ImageStream)
        {
            try
            {
                //获取ServiceConfiguration.cscfg配置文件的信息
                var account = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnectionString"));

                var client = account.CreateCloudBlobClient();

                //获得BlobContainer对象
                CloudBlobContainer blobContainer
                    = client.GetContainerReference(CloudConfigurationManager.GetSetting("ContainerName"));

                if (!blobContainer.Exists())
                {
                    // 检查container是否被创建,如果没有,创建container

                    blobContainer.CreateIfNotExists();
                    var permissions = blobContainer.GetPermissions();

                    //对Storage的访问权限是可以浏览Container
                    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                    blobContainer.SetPermissions(permissions);
                }


                Guid guid = Guid.NewGuid();
                CloudBlockBlob blob = blobContainer.GetBlockBlobReference(guid.ToString() + ".jpg");
                blob.Properties.ContentType = "image/jpeg";
                //request.Content.Headers.ContentType.MediaType

                //blob.UploadFromByteArray(content, 0, content.Length);
                blob.UploadFromStream(ImageStream);
                blob.SetProperties();

                return guid.ToString();
            }
            catch (Exception ex)
            {
                return ex.InnerException.ToString();
            }
        }
    }
}
复制代码

   以上代码和配置,实现了以下功能点:

  1.在Windows Azure Storage里创建名为photos的Container,并且设置Storage的访问权限

  2.设置上传的BlockbName为GUID

  3.通过UploadFromStream,将客户端POST的Stream保存到Azure Storage里

 

  11.修改Web.config的 <system.serviceModel>节点。设置WCF的相关配置。

复制代码
  <system.serviceModel>
    <bindings>
      <!--<webHttpBinding></webHttpBinding>-->
      <webHttpBinding>
        <binding name="WCFServiceBinding"
                 maxReceivedMessageSize="10485760"
                 maxBufferSize="10485760"
                 closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00">
          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="LeiWCFService.Service1" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="LeiWCFService.IService1"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
复制代码

  

  

  12.在VS2013离,点击Save All,并将项目发布至Windows Azure。

  DNS我们设置为LeiAzureService

  13.等待项目发布结束后,在IE浏览器中输入http://leiazureservice.cloudapp.net/service1.svc/help看到如下的图片,就证明发布成功了

  

    

 

分类:  Azure Storage

本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/p/3398818.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
22天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
13 0
|
29天前
|
Linux 数据安全/隐私保护 Docker
linux和windows中安装emqx消息服务器
linux和windows中安装emqx消息服务器
27 0
|
1月前
|
存储 Windows
windows server 2019 云服务器看不见硬盘的解决方案
windows server 2019 云服务器看不见硬盘的解决方案
|
1月前
|
数据安全/隐私保护 Windows
Windows Server 各版本搭建终端服务器实现远程访问(03~19)
左下角开始➡管理工具➡管理您的服务器,点击添加或删除角色点击下一步勾选自定义,点击下一步蒂埃涅吉终端服务器,点击下一步点击确定重新登录后点击确定点击开始➡管理工具➡计算机管理,展开本地用户和组,点击组可以发现有个组关门用来远程登录右键这个组点击属性,点击添加输入要添加的用户名,点击确定添加成功后点击确定打开另一台虚拟机(前提是在同一个局域网内),按 WIN + R 输入 mstsc 后回车输入 IP 地址后点击连接输入用户名及密码后点击确定连接成功!
32 0
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
53 2
|
1月前
|
数据安全/隐私保护 Windows
Windows Server 2003 搭建邮件服务器实现自建邮箱域名及账户并连接外网
Windows Server 2003 搭建邮件服务器实现自建邮箱域名及账户并连接外网
27 0
|
25天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
29天前
|
SQL 弹性计算 安全
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
当我们通过阿里云的活动购买完云服务器之后,并不是立马就能使用了,还需要我们设置云服务器密码,配置安全组等基本操作之后才能使用,有的用户还需要购买并挂载数据盘到云服务器上,很多新手用户由于是初次使用阿里云服务器,因此并不知道这些设置的操作流程,下面给大家介绍下这些设置的具体操作流程。
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
|
30天前
|
弹性计算
阿里云3M带宽云服务器并发多大?阿里云3M带宽云服务器测评参考
在探讨云服务器3M带宽能支持多大并发这一问题时,我们首先要明白一个关键点:并发量并非仅由带宽决定,还与网站本身的大小密切相关。一般来说,一个优化良好的普通网站页面大小可能只有几K,为便于计算,我们可以暂且假定每个页面大小为50K。
807 1