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.