public async Task <IEnumerable <EventBase> > ListInstanceEventsAsync(
            StorageObjectLocator locator,
            CancellationToken cancellationToken)
        {
            using (TraceSources.IapDesktop.TraceMethod().WithParameters(locator))
            {
                var events = new List <EventBase>();

                using (var stream = await this.storageAdapter.DownloadObjectToMemoryAsync(
                           locator,
                           cancellationToken)
                                    .ConfigureAwait(false))
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        // The file contains a sequence of JSON structures, separated
                        // by a newline.

                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            // The file might contain empty lines.
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                events.Add(EventFactory.FromRecord(LogRecord.Deserialize(line)));
                            }
                        }
                    }

                return(events);
            }
        }
Esempio n. 2
0
        public async Task <Stream> DownloadObjectToMemoryAsync(
            StorageObjectLocator locator,
            CancellationToken cancellationToken)
        {
            using (TraceSources.IapDesktop.TraceMethod().WithParameters(locator))
            {
                try
                {
                    var buffer = new MemoryStream();
                    var result = await this.service.Objects
                                 .Get(locator.Bucket, locator.ObjectName)
                                 .DownloadAsync(buffer)
                                 .ConfigureAwait(false);

                    if (result.Exception != null)
                    {
                        throw result.Exception;
                    }

                    buffer.Seek(0, SeekOrigin.Begin);
                    return(buffer);
                }
                catch (GoogleApiException e)
                    when((e.Error != null && (e.Error.Code == 403 || e.Error.Code == 404)) ||
                         e.Message == "Not Found" ||
                         e.Message.Contains("storage.objects.get access"))
                    {
                        throw new ResourceAccessDeniedException(
                                  $"Access to storage bucket {locator.Bucket} has been denied", e);
                    }
            }
        }