示例#1
0
        private static void Download(string fileName)
        {
            var file = _folderPathDownload + fileName;

            using (var fileStream = File.Create(file))
            {
                _storageClient.DownloadObject(_bucketName, fileName, fileStream);
            }
        }
示例#2
0
        public override string ReadAllText(string path)
        {
            using (var stream = new MemoryStream())
            {
                _storageClient.DownloadObject(_bucketName, path, stream);

                using (StreamReader reader = new StreamReader(stream))
                {
                    return(reader.ReadToEnd());
                }
            }
        }
示例#3
0
 public void download(string bucket_name, string key_name, Stream stream)
 {
     try
     {
         client.DownloadObject(bucket_name, key_name, stream);
         stream.Flush();
     }
     catch (Exception ex)
     {
         throw new Exception("Error while trying to download from GCP \""
                             + bucket_name + "\\" + key_name + "\". GCP error message: "
                             + ex.Message);
     }
 }
示例#4
0
        // Import CSV file from Google Storage bucket into existing Google BigQuery data table
        private void ImportFileFromStorageToBigQuery(BigqueryClient client, string projectName,
                                                     string bucketName, string fileName, string dataSetName, string tableName)
        {
            StorageClient gcsClient = StorageClient.Create(_GoogleAPICredential);

            using (var stream = new MemoryStream())
            {
                gcsClient.DownloadObject(bucketName, fileName, stream);

                // This uploads data to an existing table. If the upload will create a new table
                // or if the schema in the CSV isn't identical to the schema in the table,
                // create a schema to pass into the call instead of passing in a null value.
                BigqueryJob job = null;
                try
                {
                    job = client.UploadCsv(dataSetName, tableName, null, stream);
                }
                catch (Exception e)
                {
                    string m = e.Message;
                }
                // Use the job to find out when the data has finished being inserted into the table,
                // report errors etc.

                // Wait for the job to complete.
                try
                {
                    job.Poll();
                } catch (Exception e) {
                    string m = e.Message;
                }
            }
        }
        public StorageData Get(StorageFileId storageFileId)
        {
            var pdfBytes = new MemoryStream();

            _storageClient.DownloadObject(_settings.GoogleBucketName, GetObjectName(storageFileId), pdfBytes, null, null);
            return(new StorageData(storageFileId, pdfBytes.ToArray()));
        }
        /// <summary>
        /// Download a file object from GCS
        /// </summary>
        private void DownloadObject(StorageClient client, Google.Apis.Storage.v1.Data.Object gcsObject,
                                    FileObject fileObject, ObjectVersion version)
        {
            try
            {
                using (MemoryStream memStm = new MemoryStream())
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Helpers.WriteProgress(0);

                    _fileSize = (long)gcsObject.Size.GetValueOrDefault();
                    client.DownloadObject(gcsObject, memStm, new DownloadObjectOptions()
                    {
                        Generation = version.Generation
                    }, this);

                    memStm.Position        = 0;
                    fileObject.DataContent = memStm.ToArray();
                }
            }
            catch (Exception ex)
            {
                throw new SmkException(string.Format(ErrorResources.GcsBucketSource_DownloadingError, gcsObject.Name), ex);
            }
        }
示例#7
0
        public TagValueResponse[] GetPLCData([FromBody] RquestObj req)
        {
            TagValueResponse[] resp = null;
            try
            {
                //DateTime chrtdate_from, chrtdate_to;
                //GraphTypeDC[] objOutPutDataDCarray = null;
                //chrtdate_from = Convert.ToDateTime(FromDate);
                //chrtdate_to = Convert.ToDateTime(ToDate);
                //objOutPutDataDCarray = WcfService.objHMI2.STUB_GetAIEQuipFlag(chrtdate_from, chrtdate_to, EqpNO, Scenarios);
                //return objOutPutDataDCarray;
                string   projectId = "projamdstrial";
                DateTime chrtdate_from, chrtdate_to;

                chrtdate_from = Convert.ToDateTime(req.FromDate);
                chrtdate_to   = Convert.ToDateTime(req.ToDate);

                // Instantiates a client.
                using (StorageClient storageClient = StorageClient.Create())
                {
                    // The name for the new bucket.
                    string bucketName = "TestAMDSData/BFProcess_FBF_DATA_HEARTH_2020_1_28_20200128073415.csv";
                    try
                    {
                        // Creates the new bucket.
                        var bucket = storageClient.GetBucket("amds_bucket");
                        using (MemoryStream mem = new MemoryStream())
                        {
                            storageClient.DownloadObject("amds_bucket", bucketName, mem);
                            //StreamReader reader = new StreamReader(mem);
                            //string content = reader.ReadToEnd();
                            string        content = Encoding.ASCII.GetString(mem.ToArray());
                            string[]      sep     = { "\n" };
                            List <string> lines   = content.Split(sep, StringSplitOptions.RemoveEmptyEntries).ToList();
                            lines.RemoveAt(0);
                            List <TagValueResponse> dataList = new List <TagValueResponse>();
                            lines.ForEach(x => dataList.Add(new TagValueResponse()
                            {
                                READTIME = Convert.ToDateTime(x.Split(',')[0]),
                                TAGID    = Convert.ToInt32(x.Split(',')[1]),
                                VALUE    = Convert.ToDecimal(x.Split(',')[2])
                            }));
                            resp = dataList.Where(x => x.READTIME >= chrtdate_from && x.READTIME <= chrtdate_to && x.TAGID == req.TAGID).ToArray();
                        }
                        Console.WriteLine($"Bucket {bucketName} created.");
                    }
                    catch (Google.GoogleApiException e)
                        when(e.Error.Code == 409)
                        {
                            // The bucket already exists.  That's fine.
                            Console.WriteLine(e.Error.Message);
                        }
                }
                return(resp);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#8
0
        // [END delete_table]

        // [START import_file_from_gcs]
        public void ImportDataFromCloudStorage(string projectId, string datasetId,
                                               string tableId, BigqueryClient client, string fileName, string folder = null)
        {
            StorageClient gcsClient = StorageClient.Create();

            using (var stream = new MemoryStream())
            {
                // Set Cloud Storage Bucket name. This uses a bucket named the same as the project.
                string bucket = projectId;
                // If folder is passed in, add it to Cloud Storage File Path using "/" character
                string filePath = string.IsNullOrEmpty(folder) ? fileName : folder + "/" + fileName;
                // Download Google Cloud Storage object into stream
                gcsClient.DownloadObject(projectId, filePath, stream);

                // This example uploads data to an existing table. If the upload will create a new table
                // or if the schema in the JSON isn't identical to the schema in the table,
                // create a schema to pass into the call instead of passing in a null value.
                BigqueryJob job = client.UploadJson(datasetId, tableId, null, stream);
                // Use the job to find out when the data has finished being inserted into the table,
                // report errors etc.

                // Wait for the job to complete.
                job.PollUntilCompleted();
            }
        }
示例#9
0
        public void StartLoadingImage()
        {
            using (var stream = new MemoryStream())
            {
                try
                {
                    this.ImageObj.MediaLink = ImageObj.MediaLink + "=s32-c";
                    this.ImageObj.SelfLink  = ImageObj.SelfLink + "=s32-c";
                    //storage.Service.
                    storage.DownloadObject(ImageObj, stream);
                    stream?.Seek(0, SeekOrigin.Begin);

                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    //bitmap.DecodePixelHeight = 230;
                    bitmap.DecodePixelWidth = 230;
                    bitmap.StreamSource     = stream;
                    bitmap.CacheOption      = BitmapCacheOption.OnLoad; //BitmapCacheOption.OnLoad;
                    //bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;
                    bitmap.EndInit();
                    bitmap.Freeze();

                    //No need to wait for this to finish
                    //ImageInfo(bitmap, obj);

                    Data = bitmap;
                }
                catch (Exception ex)
                {
                    Data = new BitmapImage();
                }
            }
        }
        public BitmapImage GetBitmap(Google.Apis.Storage.v1.Data.Object obj)
        {
            using (var stream = new MemoryStream())
            {
                try
                {
                    //obj.MediaLink = obj.MediaLink + "=s32-c";
                    //obj.SelfLink = obj.SelfLink + "=s32-c";
                    //storage.Service.
                    storage.DownloadObject(obj, stream);
                    stream?.Seek(0, SeekOrigin.Begin);

                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.DecodePixelHeight = 230;
                    bitmap.DecodePixelWidth  = 230;
                    bitmap.StreamSource      = stream;
                    bitmap.CacheOption       = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();

                    //No need to wait for this to finish
                    //ImageInfo(bitmap, obj);

                    return(bitmap);
                }catch (Exception ex)
                {
                    return(new BitmapImage());
                }
            }
        }
        public void ExportCsv()
        {
            // TODO: Make this simpler in the wrapper
            var    projectId      = _fixture.ProjectId;
            var    datasetId      = _fixture.GameDatasetId;
            var    historyTableId = _fixture.HistoryTableId;
            string bucket         = "bigquerysnippets-" + Guid.NewGuid().ToString().ToLowerInvariant();
            string objectName     = "table.csv";

            if (!WaitForStreamingBufferToEmpty(historyTableId))
            {
                Console.WriteLine("Streaming buffer not empty after 30 seconds; not performing export");
                return;
            }

            // Sample: ExportCsv
            BigqueryClient client = BigqueryClient.Create(projectId);

            // Create a storage bucket; in normal use it's likely that one would exist already.
            StorageClient storageClient = StorageClient.Create();

            storageClient.CreateBucket(projectId, bucket);
            string destinationUri = $"gs://{bucket}/{objectName}";

            Job job = client.Service.Jobs.Insert(new Job
            {
                Configuration = new JobConfiguration
                {
                    Extract = new JobConfigurationExtract
                    {
                        DestinationFormat = "CSV",
                        DestinationUris   = new[] { destinationUri },
                        SourceTable       = client.GetTableReference(datasetId, historyTableId)
                    }
                }
            }, projectId).Execute();

            // Wait until the export has finished.
            var result = client.PollJob(job.JobReference);

            // If there are any errors, display them *then* fail.
            if (result.Status.ErrorResult != null)
            {
                foreach (var error in result.Status.Errors)
                {
                    Console.WriteLine(error.Message);
                }
            }
            Assert.Null(result.Status.ErrorResult);

            MemoryStream stream = new MemoryStream();

            storageClient.DownloadObject(bucket, objectName, stream);
            Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
            // End sample

            storageClient.DeleteObject(bucket, objectName);
            storageClient.DeleteBucket(bucket);
        }
        public Stream GetStream()
        {
            var outputStream = new MemoryStream();

            Client.DownloadObject(Object, outputStream);
            outputStream.Position = 0;
            return(outputStream);
        }
示例#13
0
        public async Task <Stream> GetFile(string path)
        {
            var mem = new MemoryStream();

            storage.DownloadObject(BUCKET_NAME, path, mem);
            mem.Position = 0;
            return(mem);
        }
示例#14
0
 private void TryDownloadObject(string bucket, GcsObject obj, string destinationFile)
 {
     using (var outputFile = _fileSystem.File.OpenWrite(destinationFile))
     {
         _storageClient.DownloadObject(bucket, obj.Name, outputFile);
         Console.WriteLine($"Writing: {destinationFile}");
     }
 }
            private static BenchmarkRun LoadContainer(StorageClient client, string runStorageName)
            {
                var stream = new MemoryStream();

                client.DownloadObject(BucketName, runStorageName, stream);
                stream.Position = 0;
                return(BenchmarkRun.Parser.ParseFrom(stream));
            }
示例#16
0
 static void DownloadObject(string bucketName, string objectName, string filePath = null)
 {
     filePath = filePath ?? Path.GetFileName(objectName);
     using (var stream = File.OpenWrite(filePath))
     {
         storageClient.DownloadObject(bucketName, objectName, stream);
     }
     Console.WriteLine($"{objectName}, {filePath} olarak indirildi.");
 }
示例#17
0
 private void DownloadObject(StorageClient storage, string bucketName, string objectName, string localPath = null)
 {
     localPath = localPath ?? Path.GetFileName(objectName);
     using (var outputFile = File.OpenWrite(localPath))
     {
         storage.DownloadObject(bucketName, objectName, outputFile);
     }
     Console.WriteLine($"downloaded {objectName} to {localPath}.");
 }
示例#18
0
        internal static void ValidateData(string bucketName, string objectName, byte[] expectedData,
                                          StorageClient client = null, DownloadObjectOptions options = null)
        {
            client = client ?? StorageClient.Create();
            var downloaded = new MemoryStream();

            client.DownloadObject(bucketName, objectName, downloaded, options);
            Assert.Equal(expectedData, downloaded.ToArray());
        }
示例#19
0
 public void DownloadObject(string bucketName, string objectName,
                            string localPath = null)
 {
     localPath ??= Path.GetFileName(objectName);
     using (var outputFile = File.OpenWrite(localPath))
     {
         storageClient.DownloadObject(bucketName, objectName, outputFile);
     }
     Console.WriteLine($"GoogleBucket.cs - downloaded {objectName} to {localPath}.");
 }
示例#20
0
        private async void DownloadTableData(string bucketName, string objectName)
        {
            var credential = GoogleCredential.FromFile(" enter credentials");

            StorageClient storageClient = await StorageClient.CreateAsync(credential);

            using (var fstream = new FileStream(Path.Combine(WebHostEnvironment.WebRootPath, "data", objectName), FileMode.Create))
            {
                storageClient.DownloadObject(bucketName, objectName, fstream);
            }
        }
        public Stream CreateReadStream()
        {
            // Json config files are small, and will easily fit in memory.
            var stream = new MemoryStream();

            _storage.DownloadObject(_cloudStorageObject.Bucket,
                                    _cloudStorageObject.Name, stream);
            _logger.LogInformation("Loaded {0}/{1}", _cloudStorageObject.Bucket,
                                   _cloudStorageObject.Name);
            stream.Seek(0, SeekOrigin.Begin);;
            return(stream);
        }
        private void DownloadPackage(string versionId, string localFileName)
        {
            if (File.Exists(localFileName))
            {
                File.Delete(localFileName);
            }

            using (var fileStream = File.OpenWrite(localFileName))
            {
                StorageClient.DownloadObject(Bucket, versionId, fileStream);
            }
        }
示例#23
0
 public void DownloadObject(string aBucketName, string aName, Stream aMemory)
 {
     try {
         storage.DownloadObject(aBucketName, aName, aMemory);
     }
     catch (Google.GoogleApiException e)
         when(e.Error.Code == 409)
         {
             // The bucket already exists.  That's fine.
             Console.WriteLine(e.Error.Message);
         }
 }
            private static IList <BenchmarkEnvironment> LoadEnvironments(StorageClient client)
            {
                var environments = new List <BenchmarkEnvironment>();
                var stream       = new MemoryStream();

                client.DownloadObject(BucketName, EnvironmentObjectName, stream);
                stream.Position = 0;
                while (stream.Position != stream.Length)
                {
                    environments.Add(BenchmarkEnvironment.Parser.ParseDelimitedFrom(stream));
                }
                return(environments);
            }
示例#25
0
    public void DownloadFile(string fileToDownload, StorageClient sc, string bn)
    {
        string objectName = fileToDownload;
        string localPath  = Directory.GetCurrentDirectory() + "\\" + Path.GetFileName(objectName);

        using (var outputFile = File.OpenWrite(localPath))
        {
            sc.DownloadObject(bn, objectName, outputFile);
        }
        Debug.Log($"downloaded {objectName} to {localPath}.");
        ScrollView.SetActive(false);
        LoadFile(localPath);
    }
示例#26
0
        public void LoadEnvironments()
        {
            environments.Clear();
            var stream = new MemoryStream();

            client.DownloadObject(bucketName, objectPrefix + EnvironmentObjectName, stream);
            stream.Position = 0;
            while (stream.Position != stream.Length)
            {
                environments.Add(BenchmarkEnvironment.Parser.ParseDelimitedFrom(stream));
            }
            environmentSaveRequired = false;
        }
示例#27
0
        public void DownloadFile(string fileName, Stream destinationStream, string bucketName = "")
        {
            EnsureBucketExist(bucketName);

            try
            {
                _storageClient.DownloadObject(_bucketName, fileName, destinationStream);
            }
            catch (GoogleApiException gex)
            {
                _logger.LogError(-1, gex, gex.Error.Message);
                throw;
            }
            destinationStream.Seek(0, SeekOrigin.Begin);
        }
示例#28
0
        public IActionResult GetImage([FromQuery] string name)
        {
            var stream = new MemoryStream();

            try
            {
                stream.Position = 0;
                var obj = _storageClient.GetObject("tenant-file-fc6de.appspot.com", name);
                _storageClient.DownloadObject(obj, stream);
                stream.Position = 0;

                var response = File(stream, obj.ContentType, "file.png"); // FileStreamResult
                return(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static async Task downloadFilesGoogle()
        {
            var           credential    = GoogleCredential.FromFile(@"D:\Google_API_key_TFM.json");
            StorageClient storageClient = await StorageClient.CreateAsync(credential);

            storageClient.Service.HttpClient.Timeout = new TimeSpan(0, 10, 0);
            var bucket = await storageClient.GetBucketAsync("bucket-ocr-tfm");

            var    blobList = storageClient.ListObjects(bucket.Name, "procesados/");
            string strPath  = @"D:\json\";

            foreach (var outputOcr in blobList.Where(x => x.Name.Contains(".json")))
            {
                using (var stream = File.OpenWrite(strPath + outputOcr.Name.Split('/')[1]))
                {
                    storageClient.DownloadObject(outputOcr, stream);
                }
            }
        }
        public async Task <string> LoadTextFileAsync(string filename)
        {
            Object item = _storageClient.GetObject(_configuration.BucketName, filename);

            if (item != null)
            {
                using (Stream stream = new MemoryStream())
                {
                    _storageClient.DownloadObject(item, stream);
                    stream.Position = 0;
                    using (var reader = new StreamReader(stream))
                    {
                        return(await reader.ReadToEndAsync());
                    }
                }
            }

            return("");
        }