Exemple #1
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetObjectTaggingResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 2;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("Tag", targetDepth))
                    {
                        response.Tagging.Add(TagUnmarshaller.Instance.Unmarshall(context));

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }
            return;
        }
        static void ReadObjectTags()
        {
            string id  = Common.InputString("Key:", null, false);
            int    ver = Common.InputInteger("Version:", 1, true, false);

            try
            {
                GetObjectTaggingRequest request = new GetObjectTaggingRequest();
                request.BucketName = _Bucket;
                request.Key        = id;
                request.VersionId  = ver.ToString();

                GetObjectTaggingResponse response = _S3Client.GetObjectTaggingAsync(request).Result;

                if (response != null)
                {
                    Console.WriteLine("Success");
                    foreach (Tag curr in response.Tagging)
                    {
                        Console.WriteLine("  " + curr.Key + ": " + curr.Value);
                    }
                }
                else
                {
                    Console.WriteLine("Failed");
                }
            }
            catch (Exception)
            {
                throw new IOException("Unable to read object tags.");
            }
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetObjectTaggingResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth = originalDepth + 2;
            
            if (context.IsStartOfDocument) 
               targetDepth += 2;

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("Tag", targetDepth))
                    {
                        response.Tagging.Add(TagUnmarshaller.Instance.Unmarshall(context));

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }
            return;
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            GetObjectTaggingResponse getObjectTaggingResponse = new GetObjectTaggingResponse();

            while (context.Read())
            {
                if (context.get_IsStartElement())
                {
                    UnmarshallResult(context, getObjectTaggingResponse);
                }
            }
            return(getObjectTaggingResponse);
        }
 public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) 
 {
     GetObjectTaggingResponse response = new GetObjectTaggingResponse();
     
     while (context.Read())
     {
         if (context.IsStartElement)
         {
             UnmarshallResult(context, response);
             continue;
         }
     }
     return response;
 }
        public void GetObjectTagging()
        {
            GetObjectTaggingRequest tagRequest = new GetObjectTaggingRequest
            {
                BucketName = strBucketName,
                Key        = strObjectName
            };
            GetObjectTaggingResponse objectTags = client.GetObjectTagging(tagRequest);

            if (objectTags.Tagging.Count == 0)
            {
                Console.WriteLine("No Tags Found");
            }
            foreach (var tag in objectTags.Tagging)
            {
                Console.WriteLine($"Key: {tag.Key}, Value: {tag.Value}");
            }
        }
        private async Task <string> gets3tag(IAmazonS3 s3Client, string id, string newkey)
        {
            GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest();

            getTagsRequest.BucketName = bucketName;
            getTagsRequest.Key        = id;
            GetObjectTaggingResponse objectTags = await s3Client.GetObjectTaggingAsync(getTagsRequest);

            Tag tag = objectTags.Tagging.Find(t => t.Key == "Status");

            if (tag != null)
            {
                return(tag.Value);
            }
            else
            {
                return(null);
            }
        }
        //bucketa gelen dosyaları ayırt etme
        //olayı kim koydu bu dosyayı
        public void GetObjectTagging()
        {
            GetObjectTaggingRequest request = new GetObjectTaggingRequest
            {
                BucketName = bucketName,
                Key        = "test.txt",
            };

            GetObjectTaggingResponse response = client.GetObjectTagging(request);

            if (response.Tagging.Count == 0)
            {
                Console.WriteLine("tags are not found");
            }

            foreach (var tag in response.Tagging)
            {
                Console.WriteLine($"Key:{tag.Key}, Values: {tag.Value}");
            }
            Console.ReadLine();
        }
Exemple #9
0
        public void GetObjectTagging()
        {
            GetObjectTaggingRequest tagRequest = new GetObjectTaggingRequest
            {
                BucketName = bucketName,
                Key        = "test.txt"
            };

            GetObjectTaggingResponse objectTags = client.GetObjectTagging(tagRequest);

            if (objectTags.Tagging.Count > 0)
            {
                foreach (var tag in objectTags.Tagging)
                {
                    Console.WriteLine($"Key: {tag.Key} \n Value: {tag.Value}");
                }
            }
            else
            {
                Console.WriteLine("\nNo Tags Found!\n");
            }
        }
Exemple #10
0
        private static async Task <CopyObjectResponse> Copy(string sourceBucket, string sourceKey, string destinationBucket, string prefixPattern, ILambdaContext context)
        {
            // The S3 key prefixes are separated with a forward slash
            string[] Parts          = sourceKey.Split("/");
            string   DestinationKey = String.Format(prefixPattern, Parts);
            string   DestinationUri = $"s3://{destinationBucket}/{DestinationKey}";

            context.LogInfo($"Using destination: {DestinationUri}");

            GetObjectTaggingRequest TagRequest = new GetObjectTaggingRequest()
            {
                BucketName = sourceBucket,
                Key        = sourceKey
            };

            GetObjectTaggingResponse TagResponse = await _S3Client.GetObjectTaggingAsync(TagRequest);

            CopyObjectRequest CopyRequest = new CopyObjectRequest()
            {
                DestinationBucket = destinationBucket,
                SourceBucket      = sourceBucket,
                SourceKey         = sourceKey,
                DestinationKey    = DestinationKey,
                TagSet            = TagResponse.Tagging
            };

            CopyObjectResponse Response = await _S3Client.CopyOrMoveObjectAsync(CopyRequest, true);

            if (Response.HttpStatusCode == HttpStatusCode.OK)
            {
                context.LogInfo($"Successfully moved s3://{sourceBucket}/{sourceKey} to {DestinationUri}.");
            }
            else
            {
                context.LogError($"Unsuccessful copy of s3://{sourceBucket}/{sourceKey} to {DestinationUri} : ${(int)Response.HttpStatusCode}");
            }

            return(Response);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetObjectTaggingResponse response)
        {
            int currentDepth = context.get_CurrentDepth();
            int num          = currentDepth + 2;

            if (context.get_IsStartOfDocument())
            {
                num += 2;
            }
            while (context.Read())
            {
                if (context.get_IsStartElement() || context.get_IsAttribute())
                {
                    if (context.TestExpression("Tag", num))
                    {
                        response.Tagging.Add(TagUnmarshaller.Instance.Unmarshall(context));
                    }
                }
                else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth)
                {
                    break;
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// This method uploads an object with tags. It then shows the tag
        /// values, changes the tags, and shows the new tags.
        /// </summary>
        /// <param name="client">The Initialized Amazon S3 client object used
        /// to call the methods to create and change an objects tags.</param>
        /// <param name="bucketName">A string representing the name of the
        /// bucket where the object will be stored.</param>
        /// <param name="keyName">A string representing the key name of the
        /// object to be tagged.</param>
        /// <param name="filePath">The directory location and file name of the
        /// object to be uploaded to the Amazon S3 bucket.</param>
        public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath)
        {
            try
            {
                // Create an object with tags.
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName,
                    FilePath   = filePath,
                    TagSet     = new List <Tag>
                    {
                        new Tag {
                            Key = "Keyx1", Value = "Value1"
                        },
                        new Tag {
                            Key = "Keyx2", Value = "Value2"
                        },
                    },
                };

                PutObjectResponse response = await client.PutObjectAsync(putRequest);

                // Now retrieve the new object's tags.
                GetObjectTaggingRequest getTagsRequest = new()
                {
                    BucketName = bucketName,
                    Key        = keyName,
                };

                GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest);

                // Display the tag values.
                objectTags.Tagging
                .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));

                Tagging newTagSet = new()
                {
                    TagSet = new List <Tag>
                    {
                        new Tag {
                            Key = "Key3", Value = "Value3"
                        },
                        new Tag {
                            Key = "Key4", Value = "Value4"
                        },
                    },
                };

                PutObjectTaggingRequest putObjTagsRequest = new ()
                {
                    BucketName = bucketName,
                    Key        = keyName,
                    Tagging    = newTagSet,
                };

                PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest);

                // Retrieve the tags again and show the values.
                GetObjectTaggingRequest getTagsRequest2 = new()
                {
                    BucketName = bucketName,
                    Key        = keyName,
                };
                GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2);

                objectTags2.Tagging
                .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine(
                    $"Error: '{ex.Message}'");
            }
        }
        static async Task PutObjectWithTagsTestAsync()
        {
            try
            {
                // 1. Put an object with tags.
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName,
                    FilePath   = filePath,
                    TagSet     = new List <Tag> {
                        new Tag {
                            Key = "Keyx1", Value = "Value1"
                        },
                        new Tag {
                            Key = "Keyx2", Value = "Value2"
                        }
                    }
                };

                PutObjectResponse response = await client.PutObjectAsync(putRequest);

                // 2. Retrieve the object's tags.
                GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };

                GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest);

                for (int i = 0; i < objectTags.Tagging.Count; i++)
                {
                    Console.WriteLine("Key: {0}, Value: {1}", objectTags.Tagging[i].Key, objectTags.Tagging[i].Value);
                }


                // 3. Replace the tagset.

                Tagging newTagSet = new Tagging();
                newTagSet.TagSet = new List <Tag> {
                    new Tag {
                        Key = "Key3", Value = "Value3"
                    },
                    new Tag {
                        Key = "Key4", Value = "Value4"
                    }
                };


                PutObjectTaggingRequest putObjTagsRequest = new PutObjectTaggingRequest()
                {
                    BucketName = bucketName,
                    Key        = keyName,
                    Tagging    = newTagSet
                };
                PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest);

                // 4. Retrieve the object's tags.
                GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest();
                getTagsRequest2.BucketName = bucketName;
                getTagsRequest2.Key        = keyName;
                GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2);

                for (int i = 0; i < objectTags2.Tagging.Count; i++)
                {
                    Console.WriteLine("Key: {0}, Value: {1}", objectTags2.Tagging[i].Key, objectTags2.Tagging[i].Value);
                }
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(
                    "Error encountered ***. Message:'{0}' when writing an object"
                    , e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    "Encountered an error. Message:'{0}' when writing an object"
                    , e.Message);
            }
        }
Exemple #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(S3Event input, ILambdaContext context)
        {
            // Initialize the Amazon Cognito credentials provider
            CognitoAWSCredentials credentials = new CognitoAWSCredentials(
                "us-east-1:6d711ae9-1084-4a71-9ef6-7551ca74ad0b", // Identity pool ID
                RegionEndpoint.USEast1                            // Region
                );

            dynamoDbClient = new AmazonDynamoDBClient(credentials, RegionEndpoint.USEast1);
            Table customersTbl = Table.LoadTable(dynamoDbClient, "Customer");


            AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient();
            IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USEast2);

            //Debug.WriteLine("Creating collection: " + FACE_COLLECTION_ID);
            //CreateCollectionRequest createCollectionRequest = new CreateCollectionRequest()
            //{
            //    CollectionId = FACE_COLLECTION_ID
            //};

            //CreateCollectionResponse createCollectionResponse = rekognitionClient.CreateCollectionAsync(createCollectionRequest).Result;
            //Debug.WriteLine("CollectionArn : " + createCollectionResponse.CollectionArn);
            //Debug.WriteLine("Status code : " + createCollectionResponse.StatusCode);


            foreach (var record in input.Records)
            {
                //if(!SupportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key)))
                //{
                //    Debug.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type");
                //    continue;
                //}

                Image image = new Image()
                {
                    S3Object = new Amazon.Rekognition.Model.S3Object
                    {
                        Bucket = record.S3.Bucket.Name,
                        Name   = record.S3.Object.Key
                    }
                };

                GetObjectTaggingResponse taggingResponse = s3Client.GetObjectTaggingAsync(
                    new GetObjectTaggingRequest
                {
                    BucketName = record.S3.Bucket.Name,
                    Key        = record.S3.Object.Key
                }
                    ).Result;

                Tag customerID = taggingResponse.Tagging[0];//TODO: HARDCODING!!


                IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
                {
                    Image               = image,
                    CollectionId        = FACE_COLLECTION_ID,
                    ExternalImageId     = record.S3.Object.Key,
                    DetectionAttributes = new List <String>()
                    {
                        "ALL"
                    }
                };

                IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFacesAsync(indexFacesRequest).Result;

                Debug.WriteLine(record.S3.Object.Key + " added");
                foreach (FaceRecord faceRecord in indexFacesResponse.FaceRecords)
                {
                    Debug.WriteLine("Face detected: Faceid is " + faceRecord.Face.FaceId);

                    Console.WriteLine("\nAfter Indexing, Updating FaceID of the Customer....");
                    string partitionKey = customerID.Value;

                    var customer = new Document();
                    customer["Id"] = Int32.Parse(partitionKey);
                    // List of attribute updates.
                    // The following replaces the existing authors list.
                    customer["FaceId"] = faceRecord.Face.FaceId;

                    // Optional parameters.
                    UpdateItemOperationConfig config = new UpdateItemOperationConfig
                    {
                        // Get updated item in response.
                        ReturnValues = ReturnValues.AllNewAttributes
                    };
                    Document updatedCustomer = customersTbl.UpdateItemAsync(customer, config).Result;
                    Console.WriteLine("UpdateMultipleAttributes: Printing item after updates ...");
                    PrintDocument(updatedCustomer);
                }
            }
            return;
        }
Exemple #15
0
        public void Start()
        {
            #region Initial configuration
            // Start the logger
            var logger = new Logger("S3SyncLogger");
            logger.LogInfo("Starting S3Sync");

            // Pull the things out of my app config file
            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            logger.LogInfo("Config settings read");

            // Check and make sure I have all the nessisary config settings
            foreach (string setting in _requiredConfigSettings)
            {
                if (appConfig[setting] == null)
                {
                    logger.LogError($"The variable {setting} is not set.");
                    _missingConfigSettings.Add(setting);
                }
            }

            // If there were missing settings I want to stop the program now.
            if (_missingConfigSettings.Count > 0)
            {
                logger.LogError($"The following values were not set in the app.config file: {_missingConfigSettings.ToString()}");
                return;
            }

            // Configure my s3 client with the access key and secret access key from the config
            _s3client = new AmazonS3Client(appConfig["AccessKey"], appConfig["SecretAccessKey"]);
            logger.LogInfo("S3Client Configured");

            // Set up my _listObjectRequest request once
            _listObjectsRequest.BucketName = appConfig["BucketName"];
            _listObjectsRequest.Prefix     = appConfig["S3FolderPath"];
            logger.LogInfo("ListObject interactions configured");

            // Set up the get object request once
            _getObjectRequest.BucketName = appConfig["BucketName"];
            logger.LogInfo("GetObject request configured");

            // Set up the get Tagg request once
            _getTagRequest.BucketName = appConfig["BucketName"];
            logger.LogInfo("GetObject request configured");

            // Identify my target directory
            _localPath = appConfig["LocalFolderPath"];

            #endregion

            while (true)
            {
                // Get the list of files in my S3 bucket.
                try
                {
                    logger.LogTrace("Getting a list of the objects in the bucket");
                    _listObjectsResponse = _s3client.ListObjects(_listObjectsRequest);
                }
                catch (Exception e)
                {
                    logger.LogError($"{e.Message}", e);
                    throw;
                }

                // For each of the files in the Bucket, check to see if they are in my folder
                foreach (S3Object entry in _listObjectsResponse.S3Objects)
                {
                    // Set the local full filepath
                    _fullFilePath = _localPath + "//" + entry.Key;

                    // Skip folders as they will be automatically created if they have any objects in them.
                    if (entry.Key.Last <char>().Equals('/'))
                    {
                        continue;
                    }

                    // See if the file already exists
                    if (File.Exists(_fullFilePath))
                    {
                        // If the last write time of the local file is greater do nothing
                        if (File.GetLastWriteTimeUtc(_fullFilePath) > entry.LastModified.ToUniversalTime())
                        {
                            continue;
                        }
                    }

                    // Set the key of the get object request to the object I want
                    _getObjectRequest.Key = entry.Key;
                    _getTagRequest.Key    = entry.Key;

                    // Get the taggs associated with the object and run the required logic against them.
                    try
                    {
                        // Get the taggs associated with the entry
                        logger.LogInfo(String.Format("Getting taggs for {0}.", entry.Key));
                        _getTagResponse = _s3client.GetObjectTagging(_getTagRequest);

                        // I am interested in two taggs in particular
                        // Update: Determines wether or not I should update the file
                        // KArtifactID: is an integer which will get passed to the windows event logger. This will be used to determine the alert to associate the upload with.

                        // If the Update Tag does not exist I do not want to update
                        if (!_getTagResponse.Tagging.Exists(x => x.Key == "Update"))
                        {
                            logger.LogInfo(String.Format("Taggs retrieved for {0}.", entry.Key));
                            continue;
                        }

                        // If the Update tag does exist and is note true I do not want to update
                        if (_getTagResponse.Tagging.Find(x => x.Key == "Update").Value.ToLower() != "true")
                        {
                            logger.LogInfo(String.Format("The object {0} was not updated because the Update tag was not true.", entry.Key));
                            continue;
                        }

                        // If there is no KArtifactID I want to skip it
                        if (!_getTagResponse.Tagging.Exists(x => x.Key == "KArtifactID"))
                        {
                            logger.LogInfo(String.Format("The object {0} was not updated because it did not contain the KArtifactID Tag.", entry.Key));
                            continue;
                        }

                        // If the KArtifactID cannot be converted into an int I do not want to update the file
                        try
                        {
                            _KArtifactID = Convert.ToInt32(_getTagResponse.Tagging.Find(x => x.Key == "KArtifactID").Value);
                        }
                        catch (Exception e)
                        {
                            logger.LogInfo(String.Format("The object {0} was not updated because it's KArtifactID was not an Int.", entry.Key));
                            logger.LogError($"{e.Message}", e);
                            continue;
                        }


                        // Let the logger know I finished grabbing the taggs
                        logger.LogInfo(String.Format("Taggs retrieved for {0}.", entry.Key));


                        // Preform the get object operation
                        logger.LogInfo(String.Format("Getting {0} last updated time is {1}.", entry.Key, entry.LastModified.ToUniversalTime()));
                        _getObjectResponse = _s3client.GetObject(_getObjectRequest);
                        _getObjectResponse.WriteResponseStreamToFile(_fullFilePath);
                        logger.LogInfo(String.Format("Retrieved {0}.", entry.Key));

                        // Write the key to the event log so I can trigger off it in Kaseya.
                        logger.LogInfo(string.Format("Writing to event log that {0} has completed sync", entry.Key));
                        _winEventLogger.ELog(entry.Key, _KArtifactID);
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"An error occured while trying to handle item: {entry.Key}");
                        logger.LogError($"{e.Message}", e);
                        throw e;
                    }
                }

                // I will only check every 30 seconds
                logger.LogTrace("Update complete.");
                System.Threading.Thread.Sleep(30000);
            }
        }