Você está na página 1de 3

works well;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Amazon.S3;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Messaging;
using System.Web.Script.Serialization;
using MySql.Data.MySqlClient;
using System.Net.Sockets;

namespace UploadToS3
{
public partial class UploadToS3 : ServiceBase
{
public UploadToS3()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
MessageQueueTransaction myTrans = null;
MessageQueue mQueue=null;
System.Messaging.Message myMsg=null;
try
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsN
etworkAvailable())
{
while (true)
{
mQueue = new MessageQueue(".\\private$\\filesync
hroqueue\\Journal$");
mQueue.Formatter = new XmlMessageFormatter(new Ty
pe[] { typeof(string) });
myTrans = new MessageQueueTransaction();
myTrans.Begin();
myMsg = mQueue.Receive(MessageQueueTransactionTy
pe.Automatic);//receive message
StringBuilder myData = new StringBuilder();
byte[] data = new byte[1024];
myMsg.BodyStream.Read(data, 0, 1024);
string qmsg = ASCIIEncoding.ASCII.GetString(data)
.ToString();//converting message to string format
XmlDocument xml = new XmlDocument();//loading xml
body
string xmlString = string.Copy(qmsg);
xml.LoadXml(xmlString);
XmlNodeList xmllist = xml.SelectNodes("/FileData"
);
foreach (XmlNode xn in xmllist)
{
string filePath = xn["filepath"].InnerText;//
fetching node values from xml
string fileName = xn["fileName"].InnerText;
string projectId = xn["projectId"].InnerText;
string userId = xn["userId"].InnerText;
string uploadTime = xn["uploadTime"].InnerTex
t;
string fileId = xn["fileId"].InnerText;
string patId = xn["patId"].InnerText;
int file_id = int.Parse(fileId);
int user_id = int.Parse(userId);
int project_id = int.Parse(projectId);
int pat_id = int.Parse(patId);
string fileToBackup = @filePath;
string myBucketName = "filesyncconcur";
string s3DirectoryName = "test";
string s3FileName = @fileName;
string FAT_NAME = s3DirectoryName + "/" + fil
eName;
FileInfo f = new FileInfo(filePath);
long FAT_FILE_SIZE = f.Length;
AmazonUploader myuploader = new AmazonUploade
r();
myuploader.sendMyFileToS3(fileToBackup, myBuc
ketName, s3DirectoryName, s3FileName);
myTrans.Commit();
MySqlConnection mcon = new MySqlConnection("s
erver=localhost; database=concur_ca; user=root;password=root; port=3306; ");
MySqlCommand mcd;
mcon.Open();
string update = "update file_attachment SET F
AT_HAS_UPLOADED= 1 where FAT_ID=" + file_id + "";
mcd = new MySqlCommand(update, mcon);
mcd.ExecuteNonQuery();
mcon.Close();

myuploader.InsertToDatabase(file_id, myBucke
tName, FAT_NAME, FAT_FILE_SIZE, user_id, uploadTime, project_id, pat_id, fileNam
e);
return;
}
}
}
}
catch (MessageQueueException ex)
{
if (ex.MessageQueueErrorCode == MessageQueueErrorCode.Acc
essDenied)
{
EventLog.WriteEntry("Access Denied" + ex.Message);
}
if (ex.MessageQueueErrorCode == MessageQueueErrorCode.Buf
ferOverflow)
{
EventLog.WriteEntry("Buffer Overflow:" + ex.InnerExce
ption.ToString());
}
if (ex.MessageQueueErrorCode == MessageQueueErrorCode.Tra
nsactionUsage)
{
EventLog.WriteEntry("Transactional Usage:" + ex.Messa
ge);
}
EventLog.WriteEntry(ex.Message);
}
catch (InvalidOperationException ex)
{
EventLog.WriteEntry(ex.Message);
}
catch (AmazonS3Exception ex)
{
EventLog.WriteEntry(ex.InnerException.ToString());
myTrans.Abort();
}
}
protected override void OnStop()
{
}
}
}

Você também pode gostar