亞馬遜上傳數據,現有能操作的功能有很多:庫存數量、跟蹤號、價格、商品.......
我們可以設置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 }
錯誤消息解決方案匯總:
在上傳過程中,經常會出現調用接口出現的異常,我將結合在工作中出現的異常實例。整理放出來,提供解決方案。