public async Task <long> IncrementAsync(IncrementRequest request)
        {
            var client = new S3Client(_region, _credentials);

            try
            {
                var obj = await client.GetObject(new GetObjectRequest(_region, BucketName, $"{request.PublicKey}/.private"));

                using (var stream = await obj.OpenAsync())
                    using (var reader = new StreamReader(stream))
                    {
                        if (await reader.ReadToEndAsync() != request.PrivateKey)
                        {
                            throw new InvalidOperationException();
                        }
                    }
            }
            catch
            {
                throw new InvalidOperationException();
            }

            var key = $"{request.PublicKey}/{request.Key}";

            try
            {
                var obj = await client.GetObject(new GetObjectRequest(_region, BucketName, key));

                using (var stream = await obj.OpenAsync())
                    using (var reader = new StreamReader(stream))
                    {
                        var current = long.Parse(await reader.ReadToEndAsync());

                        await client.PutObject(new PutObjectRequest(_region, BucketName, key)
                        {
                            Content = new StringContent((current + 1).ToString())
                        });

                        return(current + 1);
                    }
            }
            catch
            {
                await client.PutObject(new PutObjectRequest(_region, BucketName, key)
                {
                    Content = new StringContent("1")
                });

                return(1);
            }
        }
Exemplo n.º 2
0
        private void TryGetNewS3Crashes(int CrashCount)
        {
            int NewCrashCount = 0;

            while (NewCrashCount < CrashCount)
            {
                string SQSRecord = "<unset>";

                try
                {
                    if (!DequeueRecordSQS(out SQSRecord))
                    {
                        // Queue empty
                        break;
                    }

                    var RecordPair = SQSRecord.Split(',');
                    if (RecordPair.Length != 2)
                    {
                        CrashReporterProcessServicer.WriteFailure("TryGetNewS3Crashes: bad SQS message was " + SQSRecord);
                        CrashReporterProcessServicer.StatusReporter.IncrementCount(StatusReportingEventNames.ReadS3FileFailedEvent);
                        continue;
                    }

                    string S3BucketName          = RecordPair[0];
                    string S3Key                 = RecordPair[1];
                    string ReadableRequestString = "Bucket=" + S3BucketName + " Key=" + S3Key;

                    var ObjectRequest = new GetObjectRequest
                    {
                        BucketName = S3BucketName,
                        Key        = S3Key
                    };

                    using (Stream ProtocolBufferStream = new MemoryStream())
                    {
                        using (GetObjectResponse ObjectResponse = S3Client.GetObject(ObjectRequest))
                        {
                            using (Stream ResponseStream = ObjectResponse.ResponseStream)
                            {
                                if (!TryDecompResponseStream(ResponseStream, ProtocolBufferStream))
                                {
                                    CrashReporterProcessServicer.WriteFailure("! GZip fail in DecompResponseStream(): " + ReadableRequestString);
                                    CrashReporterProcessServicer.StatusReporter.IncrementCount(StatusReportingEventNames.ReadS3FileFailedEvent);
                                    continue;
                                }
                            }
                        }

                        NewCrashCount += UnpackRecordsFromDelimitedProtocolBuffers(ProtocolBufferStream, LandingZone, ReadableRequestString);
                    }
                }
                catch (Exception ex)
                {
                    CrashReporterProcessServicer.StatusReporter.IncrementCount(StatusReportingEventNames.ReadS3FileFailedEvent);
                    CrashReporterProcessServicer.WriteException("TryGetNewS3Crashes: failure during processing SQS record " + SQSRecord +
                                                                "\n" + ex);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieves an object from S3. (Caller can use the response to read the response stream)
        /// </summary>
        /// <param name="BucketName">S3 bucket name</param>
        /// <param name="Key">S3 object key</param>
        /// <returns>S3 object response for the requested object or null if it fails.</returns>
        public GetObjectResponse GetS3Object(string BucketName, string Key)
        {
            if (!IsS3Valid)
            {
                return(null);
            }

            GetObjectRequest ObjectRequest = new GetObjectRequest
            {
                BucketName = BucketName,
                Key        = Key
            };

            return(S3Client.GetObject(ObjectRequest));
        }
        public override string CopyAndExtractToTempFolder(string versionId, PackageInfo packageInfo, string tempFolder)
        {
            var @object        = S3Client.GetObject(Bucket, versionId);
            var objectFileName = Path.GetFileName(@object.Key);

            if (objectFileName == null)
            {
                throw new InvalidOperationException($"Could not extract file name from object key {@object.Key}");
            }
            var localFileName = Path.Combine(tempFolder, objectFileName);

            @object.WriteResponseStreamToFile(localFileName);

            Extract(localFileName, tempFolder, packageInfo.InternalPath);

            File.Delete(localFileName);

            return(Path.Combine(tempFolder, packageInfo.InternalPath));
        }
        public async Task <long> GetAsync(GetRequest request)
        {
            var client = new S3Client(_region, _credentials);
            var key    = $"{request.PublicKey}/{request.Key}";

            try
            {
                var obj = await client.GetObject(new GetObjectRequest(_region, BucketName, key));

                using (var stream = await obj.OpenAsync())
                    using (var reader = new StreamReader(stream))
                    {
                        return(long.Parse(await reader.ReadToEndAsync()));
                    }
            }
            catch
            {
                return(0);
            }
        }
Exemplo n.º 6
0
        public override AsimovVersion GetVersion(string versionId, PackageInfo packageInfo)
        {
            var @object = S3Client.GetObject(Bucket, versionId);

            return(ParseVersion(@object.Key, @object.LastModified));
        }