Ejemplo n.º 1
0
        public async Task UploadDocument(IFormFile formFile, Document entity, string sessionUserId)
        {
            try
            {
                var awsSection = _configuration.GetSection("AWSSettings");

                AWSObject awsconfigurations = new AWSObject
                {
                    AWSAccessKey  = awsSection["AWSAccessKey"],
                    AWSSecretKey  = awsSection["AWSSecretKey"],
                    BucketName    = awsSection["BucketName"],
                    SessionUserId = sessionUserId
                };
                var parsedContentDisposition = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition);

                var document = new DocumentViewModel
                {
                    SessionUserId    = awsconfigurations.SessionUserId,
                    DocumentTypeId   = entity.DocumentTypeId,
                    DocumentNameGuid = Guid.NewGuid().ToString() + Path.GetExtension(parsedContentDisposition.FileName.TrimStart('\"').TrimEnd('\"')),
                    Name             = parsedContentDisposition.FileName.TrimStart('\"').TrimEnd('\"')
                };

                await document.DocumentNameGuid.UploadS3FileAsync(formFile, awsconfigurations.AWSAccessKey, awsconfigurations.AWSSecretKey, awsconfigurations.BucketName);

                document.ToEntity(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public async Task DeleteEntity(int documentId)
        {
            try
            {
                var awsSection = _configuration.GetSection("AWSSettings");

                AWSObject awsconfigurations = new AWSObject
                {
                    AWSAccessKey = awsSection["AWSAccessKey"],
                    AWSSecretKey = awsSection["AWSSecretKey"],
                    BucketName   = awsSection["BucketName"],
                };


                var entity = await _context.Document.FirstOrDefaultAsync(a => a.Id == documentId);

                _context.Document.Remove(entity);
                await _context.SaveChangesAsync();

                await entity.DocumentNameGuid.Delete(awsconfigurations.AWSAccessKey, awsconfigurations.AWSSecretKey, awsconfigurations.BucketName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 3
0
        public async Task UploadDocument(IFormFile formFile, Document entity, string sessionUserId, string documentTypeDiscr)
        {
            try
            {
                var awsSection = _configuration.GetSection("AWSSettings");

                AWSObject awsconfigurations = new AWSObject
                {
                    AWSAccessKey  = awsSection["AWSAccessKey"],
                    AWSSecretKey  = awsSection["AWSSecretKey"],
                    BucketName    = awsSection["BucketName"],
                    SessionUserId = sessionUserId
                };

                var docType = await _context.DocumentType.FirstOrDefaultAsync(a => a.Discriminator == documentTypeDiscr);

                if (docType == null)
                {
                    throw new ArgumentNullException(nameof(DocumentType));
                }

                var document = new DocumentViewModel
                {
                    SessionUserId    = awsconfigurations.SessionUserId,
                    DocumentTypeId   = docType.Id,
                    DocumentNameGuid = Guid.NewGuid().ToString()
                };


                var parsedContentDisposition = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition);
                document.Name = parsedContentDisposition.FileName.TrimStart('\"').TrimEnd('\"');

                await document.DocumentNameGuid.UploadS3FileAsync(formFile, awsconfigurations.AWSAccessKey, awsconfigurations.AWSSecretKey, awsconfigurations.BucketName);

                document.ToEntity(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 4
0
        private void DoGetObject(string bucket, string key, bool getmeta, bool getdata, out AWSObject obj, out HttpWebResponse res)
        {
            string time = GetCurrentTime();
            string s2s  = (getdata ? "GET\n" : "HEAD\n") +
                          "\n" +
                          "\n" +
                          "\n" +
                          "x-amz-date:" + time + "\n" +
                          HttpUtility.UrlPathEncode("/" + bucket + "/" + key);

            string sig = MakeSignature(s2s);

            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(SERVICE_URL + HttpUtility.UrlPathEncode(bucket + "/" + key));
            req.Method                   = (getdata ? "GET" : "HEAD");
            req.ReadWriteTimeout         = -1;
            req.Timeout                  = -1;
            req.Headers["x-amz-date"]    = time;
            req.Headers["Authorization"] = "AWS " + _accessKeyId + ":" + sig;

            res = (HttpWebResponse) req.GetResponse();

            obj = new AWSObject();
            obj.Key          = key;
            obj.LastModified = res.LastModified;

            if(getmeta) {
                List<AWSMetadataEntry> retmeta = new List<AWSMetadataEntry>();
                foreach(string hdr in res.Headers.AllKeys) {
                    if(hdr.StartsWith("x-amz-meta-")) {
                        string mkey = hdr.Substring(11);
                        string mval = res.Headers[hdr];
                        if(mkey != "content-type") {
                            AWSMetadataEntry meta = new AWSMetadataEntry();
                            meta.MetaKey   = mkey;
                            meta.MetaValue = mval;
                            retmeta.Add(meta);
                        }
                    }
                }
                if(res.Headers["Content-Type"] != null) {
                    AWSMetadataEntry meta = new AWSMetadataEntry();
                    meta.MetaKey   = "Content-Type";
                    meta.MetaValue = res.Headers["Content-Type"];
                    retmeta.Add(meta);
                }
                obj.Metadata = retmeta.ToArray();
            } else {
                obj.Metadata = new AWSMetadataEntry[0];
            }

            if(getdata) {
                obj.Size = res.ContentLength;
            }
        }
Ejemplo n.º 5
0
        AWSBucket IStorageService.ListBucket(string bucket, string prefix, string marker, int? maxkeys, string delimiter)
        {
            string time = GetCurrentTime();
            string s2s  = "GET\n" +
                          "\n" +
                          "\n" +
                          "\n" +
                          "x-amz-date:" + time + "\n" +
                          HttpUtility.UrlPathEncode("/" + bucket + "/");

            string sig = MakeSignature(s2s);
            string qry = "";

            if(prefix != null)    qry = qry + (qry.Length==0 ? "?" : "&") + "prefix="    + prefix;
            if(marker != null)    qry = qry + (qry.Length==0 ? "?" : "&") + "marker="    + marker;
            if(maxkeys.HasValue)  qry = qry + (qry.Length==0 ? "?" : "&") + "max-keys="  + maxkeys.Value;
            if(delimiter != null) qry = qry + (qry.Length==0 ? "?" : "&") + "delimiter=" + delimiter;

            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(SERVICE_URL + HttpUtility.UrlPathEncode(bucket + "/") + qry);
            req.Headers["x-amz-date"]    = time;
            req.Headers["Authorization"] = "AWS " + _accessKeyId + ":" + sig;

            HttpWebResponse res = (HttpWebResponse) req.GetResponse();
            AWSBucket       ret = new AWSBucket();

            try {
                if(res.ContentType != "application/xml" && res.ContentType != "text/xml") {
                    throw new Exception("Response does not contain XML data.");
                }

                XmlDocument xml = new XmlDocument();
                xml.Load(res.GetResponseStream());

                XPathNavigator nav = xml.CreateNavigator();
                nav.MoveToChild("ListBucketResult", SERVICE_NS1);

                if(nav.MoveToChild("IsTruncated", SERVICE_NS1)) {
                    ret.IsTruncated = nav.ValueAsBoolean;
                    nav.MoveToParent();
                }

                if(nav.MoveToChild("Contents", SERVICE_NS1)) {
                    List<AWSObject> retobj = new List<AWSObject>();
                    do {
                        AWSObject obj = new AWSObject();
                        if(nav.MoveToChild("Key", SERVICE_NS1)) {
                            obj.Key = nav.Value;
                            nav.MoveToParent();
                        }
                        if(nav.MoveToChild("Size", SERVICE_NS1)) {
                            obj.Size = nav.ValueAsLong;
                            nav.MoveToParent();
                        }
                        if(nav.MoveToChild("LastModified", SERVICE_NS1)) {
                            obj.LastModified = nav.ValueAsDateTime;
                            nav.MoveToParent();
                        }
                        retobj.Add(obj);
                    } while(nav.MoveToNext("Contents", SERVICE_NS1));
                    nav.MoveToParent();
                    ret.Objects = retobj.ToArray();
                } else {
                    ret.Objects = new AWSObject[0];
                }

                if(nav.MoveToChild("CommonPrefixes", SERVICE_NS1)) {
                    List<string> retpref = new List<string>();
                    do {
                        if(nav.MoveToChild("Prefix", SERVICE_NS1)) {
                            retpref.Add(nav.Value);
                            nav.MoveToParent();
                        }
                    } while(nav.MoveToNext("CommonPrefixes", SERVICE_NS1));
                    nav.MoveToParent();
                    ret.ObjectPrefixes = retpref.ToArray();
                } else {
                    ret.ObjectPrefixes = new string[0];
                }

                return ret;

            } finally {
                res.Close();
            }
        }
Ejemplo n.º 6
0
        private AWSObject DoGetObject(string bucket, string key, bool getmeta, bool getdata)
        {
            DateTime time = GetCurrentTimeResolvedToMillis();
            string   sig  = MakeSignature("GetObject", time);
            string   cred = null;

            GetObjectResult res = _service.GetObject(bucket, key, getmeta, getdata, true, _accessKeyId, time, true, sig, cred);
            AWSObject       ret = new AWSObject();

            ret.Key          = key;
            ret.Size         = (res.Data != null) ? res.Data.Length : 0;
            ret.LastModified = res.LastModified;
            ret.Data         = res.Data;

            if(res.Metadata != null) {
                ret.Metadata = new AWSMetadataEntry[res.Metadata.Length];
                for(int idx = 0; idx < res.Metadata.Length; idx++) {
                    MetadataEntry res1 = res.Metadata[idx];
                    ret.Metadata[idx] = new AWSMetadataEntry();
                    ret.Metadata[idx].MetaKey   = res1.Name;
                    ret.Metadata[idx].MetaValue = res1.Value;
                }
            } else {
                ret.Metadata = new AWSMetadataEntry[0];
            }

            return ret;
        }