User Tools

Site Tools


source_code_naipdatadownloadutility_cs_160922.zip

Source Code NaipDataDownloadUtility_cs.zip

Model/NaipDownload.cs

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace NaipDownloaderUtility
{
    public class NaipDownload
    {
        private Collection<string> objectPaths;
        private string downloadPath;
        private string currentObjectPath;
        private int currentObjectIndex;
 
        public event EventHandler<ObjectDownloadingNaipDownloadEventArgs> ObjectDownloaded;
        public event EventHandler<DownloadCompletedNaipDownloadEventArgs> DownloadCompleted;
 
        private Amazon.RegionEndpoint regionEndpoint;
        private string bucketName;
        private AmazonS3Client client;
 
 
        public Collection<string> ObjectPaths
        {
            get
            {
                return objectPaths;                
            }
        }
 
        public string DownloadPath
        {
            get
            {
                return downloadPath;
            }
        }        
 
        public string CurrentObjectPath
        {
            get
            {
                return currentObjectPath;
            }
        }
 
        public int CurrentObjectIndex
        {
            get
            {
                return currentObjectIndex;
            }
        }
 
        public NaipDownload(IEnumerable<string> objectPaths, string downloadPath, string accessKey, string secretAccessKey)
        {
            this.objectPaths = new Collection<string>();
 
            foreach(string path in objectPaths)
            {
                this.objectPaths.Add(path);
            }
 
            this.downloadPath = downloadPath;
 
            regionEndpoint = Amazon.RegionEndpoint.USWest2;
            bucketName = "thinkgeo-naip";
 
            // We create the client token based on the access key and secret access you passed in.
            client = new AmazonS3Client(accessKey, secretAccessKey, regionEndpoint);
 
            client.BeforeRequestEvent += Client_BeforeRequestEvent;
        }
 
        private void Client_BeforeRequestEvent(object sender, Amazon.Runtime.RequestEventArgs e)
        {
            if (e is Amazon.Runtime.WebServiceRequestEventArgs)
            {
                Amazon.Runtime.WebServiceRequestEventArgs args = (Amazon.Runtime.WebServiceRequestEventArgs)e;
                args.Headers.Add("x-amz-request-payer", "requester");
            }
        }
 
        public void StartDownload()
        {            
            Thread thread = new Thread(DownloadFiles);
 
            thread.Start();
 
        }
 
 
        private void DownloadFiles()
        {
            DownloadCompletedNaipDownloadEventArgs completeArgs = new DownloadCompletedNaipDownloadEventArgs();
            completeArgs.Successful = true;
            try
            {
                for (int i = 0; i < objectPaths.Count; i++)
                {
                    currentObjectIndex = i;
                    currentObjectPath = objectPaths[currentObjectIndex];
 
                    ObjectDownloadingNaipDownloadEventArgs args = new ObjectDownloadingNaipDownloadEventArgs();
                    OnObjectDownloading(args);
 
                    if (args.Cancel)
                    {
                        completeArgs.Successful = false;
                        completeArgs.Message = "Canceled";
                        break;
                    }
 
                    DownloadFile(currentObjectPath);
 
 
                }
            }
            catch (Exception ex)
            {
 
                completeArgs.Successful = false;
                completeArgs.Message = ex.Message;
            }
            finally
            {
                OnDownloadCompleted(completeArgs);
            }
        }
 
        private void DownloadFile(string currentObjectPath)
        {
            string destinationPathFileName = downloadPath + currentObjectPath.Split('\\')[2];
 
            // If the file already exists in the cache just skip it
            if (!File.Exists(destinationPathFileName))
            {
                // Request the object based on the location in the shapefile and save it to the download path
                string key = currentObjectPath.Replace('\\', '/');
 
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = key
                };
 
 
                using (GetObjectResponse response = client.GetObject(request))
                {
                    Console.WriteLine(string.Format("Writing {0}", destinationPathFileName));
                    response.WriteResponseStreamToFile(destinationPathFileName);
                }
            }
            else
            {
                Console.WriteLine(string.Format("Skipping {0}", destinationPathFileName));
            }
 
        }
 
        private void OnDownloadCompleted(DownloadCompletedNaipDownloadEventArgs args)
        {
            EventHandler<DownloadCompletedNaipDownloadEventArgs> handler = DownloadCompleted;
            if (handler != null)
            {
                handler(this, args);
            }
        }
 
        private void OnObjectDownloading(ObjectDownloadingNaipDownloadEventArgs args)
        {
            EventHandler<ObjectDownloadingNaipDownloadEventArgs> handler = ObjectDownloaded;
            if (handler != null)
            {
                handler(this, args);
            }
        }
    }
}
source_code_naipdatadownloadutility_cs_160922.zip.txt · Last modified: 2016/09/22 20:19 by jd