https://gitlab.com/LondonAppDev/recipe-app-api-devops
Google Cloud Storage
Object storage solution in GCP
Unstructured Data storage
>> Image
>> Video
>> Binary File, etc…
Cloud storage can be used for long term archival storage
Can be access object over http, Rest API
Let’s see in action
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-106.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-107.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-108.png)
Với option location type, thì chúng ta có những tuỳ chọn khác
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-110.png)
thì bạn chỉ có thể area lớn như us, eu, asia
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-111.png)
thì ta chọn được 2 khu vực 1 lúc
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-113.png)
mình khá là nhiều product sẽ chọn cách này
VN thì hay chọn region singapore
![](https://nimtechnology.com/wp-content/uploads/2022/04/Screen-Shot-2022-04-06-at-23.53.17.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/Screen-Shot-2022-04-06-at-23.54.44.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-114.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-115.png)
Giờ click create thôi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-116.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-117.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-118.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-119.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-120.png)
Google Cloud Storage with Terraform
Create GCS and upload file via terraform
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-121.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-122.png)
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket
Bạn tạo 1 thư mục GCS
trong GCS tạo file provider.tf
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.85.0"
}
}
}
provider "google" {
# Configuration options
project = "terraform-gcp-326702"
region = "us-central1"
zone = "us-central1-a"
credentials = "keys.json"
}
Giờ tạo file main.tf
resource "google_storage_bucket" "GCS1" {
name = "tf-course-bucket-from-terraform"
storage_class = "NEARLINE"
location = "US-CENTRAL1"
labels = {
"env" = "tf_env"
"dep" = "complience"
}
uniform_bucket_level_access = true
lifecycle_rule {
condition {
age = 5
}
action {
type = "SetStorageClass"
storage_class = "COLDLINE"
}
}
retention_policy {
is_locked = true
retention_period = 864000
}
}
resource "google_storage_bucket_object" "picture" {
name = "vodafone_logo"
bucket = google_storage_bucket.GCS1.name
source = "vodafone.jpg"
}
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-123.png)
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_object
link trên sẽ giải thích cho bạn
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-124.png)
Sau khi chạy terraform apply
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-125.png)
Nếu bây giờ anh em delete bucket lỗi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-128.png)
vì trong code chúng ta có retention_policy
– (Optional) Configuration of the bucket’s data retention policy for how long objects in the bucket should be retained. Structure is documented below.
The retention_policy
block supports:
is_locked
– (Optional) If set totrue
, the bucket will be locked and permanently restrict edits to the bucket’s retention policy. Caution: Locking a bucket is an irreversible action.retention_period
– (Required) The period of time, in seconds, that objects in the bucket must be retained and cannot be deleted, overwritten, or archived. The value must be less than 2,147,483,647 seconds.
Giờ chúng ta cần làm gì?
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-129.png)