Amzon MWS API开发之 上传数据

亚马逊上传数据,现有能操作的功能有很多:库存数量、跟踪号、价格、商品.......

我们可以设置FeedType值,根据需要,再上传对应的xml文件即可。

下面可以看看FeedType类型

这次我们拿同步价格为例子,首先我们来熟悉一下Amazon MWS 提供的上传接口实现流程。

详细流程可访问:http://docs.developer.amazonservices.com/zh_CN/feeds/Feeds_Overview.html

上传流程:

在此,简要说明一下大致的步骤和流程:

第一步:建立请求

    通过MWS提供的XML程序开发指南上,根据需求,找到对应的XSD和XML实例。XML程序开发指南下载地址:点击下载

    通过程序对XML进行拼接,生存一个XML文件,保存在本地。

    调用MWS客户端的SubmitFeed方法建立一个请求,设置FeedContent值为我们拼接生存的XML文件的流。

    在建立请求后,亚马逊接受到请求后,会返回一个FeedSubmissionId值。

第二步:上传数据

    调用GetFeedSubmissionList接口方法,将第一步操作返回的FeedSubmissionId值,设置到请求参数FeedSubmissionIdList中。

      此刻,获得Amazon的返回结果,我们可以通过FeedProcessingStatusList状态来判断数据是否上传完成。

           当状态为" _DONE_" 时,说明已经上传成功,接着执行后续操作了。

    当状态为" _IN_PROGRESS_" ,此刻正在上次数据,如果数据量大的情况下,我建议大家Sleep 一会,个人建议Sleep时间设置为1—5分钟之间,视个人情况而定。

第三步:接受上传结果

          在第二步的上传状态返回" _DONE_"之后,我们可以调用GetFeedSubmissionResult方法,设置第一步返回的FeedSubmissionId参数,来获得上传结果信息。

          上传结果信息包含成功个数,失败的具体信息等。通过核对失败的信息,我们修改后可以继续上传。

           这就是整个的流程,没以生硬的MWS文档来讲解,希望大家能够理解这么一个流程。

 实例DEMO:

  1    /// <summary>
  2     /// 上传数据客户端
  3     /// </summary>
  4     public class FeedClient
  5     {
  6 
  7         private FeedClient() { }
  8 
  9         public FeedClient(string feedType)
 10         {
 11             this.FeedType = feedType;
 12         }
 13 
 14         /// <summary>
 15         /// 上传类型
 16         /// </summary>
 17         string FeedType { get; set; }
 18 
 19         /// <summary>
 20         /// 获得账户信息
 21         /// </summary>
 22         Account Account { get; set; }
 23 
 24         private MarketplaceWebServiceConfig GetConfig()
 25         {
 26             var config = new MarketplaceWebServiceConfig();
 27             config.ServiceURL = Account.ServiceUrl;
 28             return config;
 29         }
 30 
 31         private MarketplaceWebServiceClient GetClient()
 32         {
 33             var config = this.GetConfig();
 34             var client = new MarketplaceWebServiceClient(Account.AppName,
 35               Account.AppVersion, Account.AccessKeyId, Account.SecretAccessKey, config);
 36             return client;
 37         }
 38 
 39         /// <summary>
 40         /// Step 1:  提交XML或txt 上传文件,亚马逊服务端接受到数据,返回一个FeedSubmissionId
 41         /// </summary>
 42         /// <returns></returns>
 43         public string SubmitFeed()
 44         {
 45             var client = GetClient();
 46             var request = new SubmitFeedRequest();
 47             request.FeedType = this.FeedType;       //!上传商品数据
 48             request.MarketplaceIdList = new IdList();
 49             request.MarketplaceIdList.Id = new List<string> { Account.MarketplaceId };
 50 
 51             request.Merchant = Account.MerchantId;
 52             string filePath = @"D:HUAGE.txt"; //PathHelper.CreateFile(Account.AppName, "FeedContent");
 53             request.FeedContent = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
 54             request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent);
 55             request.FeedContent.Position = 0;
 56 
 57             var response = client.SubmitFeed(request);
 58             var result = response.SubmitFeedResult;
 59             return result.FeedSubmissionInfo.FeedSubmissionId;
 60         }
 61 
 62         /// <summary>
 63         /// Step 2: 提交一个SubmissionList,等待亚马逊返回"_DONE"状态,如果没有返回则一直等待。
 64         /// </summary>
 65         /// <param name="feedSubmissionId">feedSubmissionId</param>
 66         /// <returns></returns>
 67         public bool GetFeedSubmissionList(string feedSubmissionId)
 68         {
 69             bool isSuccess = true;
 70             var client = GetClient();
 71             var request = new GetFeedSubmissionListRequest();
 72             request.FeedSubmissionIdList = new IdList();
 73             request.FeedSubmissionIdList.Id = new List<string> { feedSubmissionId };
 74 
 75             while (isSuccess)
 76             {
 77                 var response = client.GetFeedSubmissionList(request);
 78                 var result = response.GetFeedSubmissionListResult;
 79 
 80                 foreach (var item in result.FeedSubmissionInfo)
 81                 {
 82                     if (item.FeedProcessingStatus == "_Done")
 83                     {
 84                         isSuccess = false;
 85                     }
 86                     else
 87                     {
 88                         System.Threading.Thread.Sleep(2 * 60 * 1000);   //! 休息一会。 
 89                     }
 90                 }
 91             }
 92             return isSuccess;
 93         }
 94 
 95         /// <summary>
 96         ///  Step 3: 获得上传结果,如果没有错,亚马逊服务端返回处理报告,否则返回错误的上传数据内容。
 97         /// </summary>
 98         /// <param name="feedSubmissionId">feedSubmissionId</param>
 99         /// <returns></returns>
100         public bool GetFeedSubmissionResult(string feedSubmissionId)
101         {
102             var client = GetClient();
103             var request = new GetFeedSubmissionResultRequest();
104             request.FeedSubmissionId = feedSubmissionId;
105             string filePath = PathHelper.CreateFile(Account.AppName, "FeedResult");
106             request.FeedSubmissionResult = File.Open(filePath, FileMode.Open, FileAccess.Read);
107             request.Merchant = Account.MerchantId;
108 
109             var response = client.GetFeedSubmissionResult(request);
110             if (response.IsSetGetFeedSubmissionResultResult())
111             {
112                 var result = response.GetFeedSubmissionResultResult;
113                 if (result.IsSetContentMD5())
114                 {
115                     return true;
116                 }
117             }
118             return false;
119         }
120 
121         /// <summary>
122         /// 整合上传数据功能
123         /// </summary>
124         public bool SubmitFile()
125         {
126             var feedSubmissionId = SubmitFeed();
127             if (!string.IsNullOrEmpty(feedSubmissionId))
128             {
129                 if (GetFeedSubmissionList(feedSubmissionId))
130                 {
131                     return GetFeedSubmissionResult(feedSubmissionId);
132                 }
133             }
134             return false;
135         }
136     }

错误消息解决方案汇总:

      在上传过程中,经常会出现调用接口出现的异常,我将结合在工作中出现的异常实例。整理放出来,提供解决方案。

原文地址:https://www.cnblogs.com/yangda/p/3963205.html