How to refresh with replace object in GCP Storage

129 views
Skip to first unread message

Dharmesh Sharma

unread,
Sep 10, 2020, 10:45:44 PM9/10/20
to Google Cloud Filestore Discuss
I have enable the google Cloud storage version for replace same name image like but after 3-4 min its refresh or sometime not refresh. I am using .net core to manage replace but not getting solution. Problem is user can upload image same name image anytime. Here is my code. 
public virtual void UploadGoogle(string localPath, string location, string contentType) { try { bool cdnStatus = Convert.ToBoolean(_configuration.GetSection("AppSetting")["GoogleCdnStatus"]); if (cdnStatus) { var gcpPath = _configuration.GetSection("GoogleCloudStorage")["fileName"]; var gcpCredential = Path.Combine(_hostingEnvironment.ContentRootPath, gcpPath); var gCPStorageCredential = GoogleCredential.FromFile(gcpCredential); // Instantiates a client. using (StorageClient storageClient = StorageClient.Create(gCPStorageCredential)) { // The name for the new bucket. string bucketName = _configuration.GetSection("AppSetting")["Googlebucket"]; using (var f = System.IO.File.OpenRead(localPath)) { //objectName = objectName ?? Path.GetFileName(localPath); //UploadObjectOptions uploadObjectOptions = new UploadObjectOptions(); //uploadObjectOptions. //if (localPath.Contains("Profile")) //{ // //storageClient.DeleteObject(bucketName, location); // try // { // Bucket bucket = new Bucket(); // bucket = storageClient.GetBucket(bucketName); // Bucket.VersioningData versioningData = new Bucket.VersioningData(); // versioningData.Enabled = true; // bucket.Versioning = versioningData; // storageClient.UpdateBucket(bucket); // } // catch (Exception ex) // { // } // storageClient.UploadObject(bucketName, location, contentType, f); //} //else //{ // storageClient.UploadObject(bucketName, location, contentType, f); //} Bucket bucket = new Bucket(); bucket = storageClient.GetBucket(bucketName); Bucket.VersioningData versioningData = new Bucket.VersioningData(); versioningData.Enabled = false; bucket.Versioning = versioningData; storageClient.UpdateBucket(bucket); storageClient.UploadObject(bucketName, location, contentType, f); } //try //{ // // Creates the new bucket. // storageClient.CreateBucket(projectId, bucketName); // 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); //} } } } catch (Exception ex) { } }

 
I have tried to delete object and upload again but same thing happened. I talked with Google Support but they don't give idea, actually why this happening because they simply saying they can't help in programming. If you see in code I have tried version enable or disable both cases. 

Olusayo Akinlaja

unread,
Sep 15, 2020, 11:01:04 AM9/15/20
to Google Cloud Filestore Discuss
Hello, Dharmesh

I think you may be able to get better traction if you post your Code on Stackoverflow, where you may find Developers that may have encountered similar issues in the past. Additionally, there are GCP Support Engineers that would be willing to help with your code on Stackoverflow. 

With that being said, the format of the code on this thread makes it so difficult to read and understand. I attempted to format it for better understanding but it is difficult for me as I could not determine which part of the Code was commented. Hence, if you would like to get advice on your Code, perhaps you can better format the code. 

Thank you
Reply all
Reply to author
Forward
0 new messages