ImageMagick 튜토리얼(1세대)


이 튜토리얼에서는 Cloud Run Functions, Cloud Vision API, ImageMagick을 사용하여 Cloud Storage 버킷에 업로드되는 이미지 중 불쾌감을 주는 이미지를 감지하고 이를 흐리게 처리하는 방법을 설명합니다.

목표

  • 저장소에서 트리거한 백그라운드 Cloud Run 함수를 배포합니다.
  • Vision API를 사용하여 폭력적인 콘텐츠 또는 성인 콘텐츠 탐지하기
  • ImageMagick을 사용하여 불쾌감을 주는 이미지를 흐리게 처리합니다.
  • 살점을 뜯어먹는 좀비 이미지를 업로드하여 해당 함수 테스트하기

비용

이 문서에서는 비용이 청구될 수 있는 다음과 같은 Google Cloud 구성요소를 사용합니다.

  • Cloud Run functions
  • Cloud Storage
  • Cloud Vision

프로젝트 사용량을 기준으로 예상 비용을 산출하려면 가격 계산기를 사용하세요. Google Cloud를 처음 사용하는 사용자는 무료 체험판을 사용할 수 있습니다.

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  4. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  9. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.

    Enable the APIs

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. gcloud CLI가 이미 설치되어 있으면 다음 명령어를 실행하여 업데이트합니다.

    gcloud components update
  13. 개발 환경을 준비합니다.

데이터 흐름 시각화

ImageMagick 튜토리얼 애플리케이션의 데이터 흐름 단계는 다음과 같습니다.

  1. 이미지가 Cloud Storage 버킷에 업로드됩니다.
  2. 이 함수는 Vision API를 사용하여 이미지를 분석합니다.
  3. 폭력적인 콘텐츠 또는 성인 콘텐츠가 감지될 경우 함수는 ImageMagick을 사용하여 해당 이미지를 흐리게 처리합니다.
  4. 흐리게 처리된 이미지는 다른 Cloud Storage 버킷에 업로드되어 사용됩니다.

애플리케이션 준비

  1. 이미지를 업로드할 Cloud Storage 버킷을 만듭니다. YOUR_INPUT_BUCKET_NAME은 전역적으로 고유한 버킷 이름입니다.

    gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
  2. 흐리게 처리된 이미지를 수신할 Cloud Storage 버킷을 만듭니다. YOUR_OUTPUT_BUCKET_NAME은 전역적으로 고유한 버킷 이름입니다.

    gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
  3. 샘플 앱 저장소를 로컬 머신에 클론합니다.

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    C#

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    Ruby

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

    PHP

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git

    또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

  4. Cloud Run Functions 샘플 코드가 포함된 디렉터리로 변경합니다.

    Node.js

    cd nodejs-docs-samples/functions/imagemagick/

    Python

    cd python-docs-samples/functions/imagemagick/

    Go

    cd golang-samples/functions/imagemagick/

    Java

    cd java-docs-samples/functions/imagemagick/

    C#

    cd dotnet-docs-samples/functions/imagemagick/

    Ruby

    cd ruby-docs-samples/functions/imagemagick/

    PHP

    cd php-docs-samples/functions/imagemagick/

코드 이해하기

종속 항목 가져오기

애플리케이션은 Google Cloud 서비스, ImageMagick, 파일 시스템과 상호 작용하기 위해 몇 가지 종속 항목을 가져와야 합니다.

Node.js

const gm = require('gm').subClass({imageMagick: true});
const fs = require('fs').promises;
const path = require('path');
const vision = require('@google-cloud/vision');

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const client = new vision.ImageAnnotatorClient();

const {BLURRED_BUCKET_NAME} = process.env;

Python

import os
import tempfile

from google.cloud import storage, vision
from wand.image import Image

storage_client = storage.Client()
vision_client = vision.ImageAnnotatorClient()

Go


// Package imagemagick contains an example of using ImageMagick to process a
// file uploaded to Cloud Storage.
package imagemagick

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"os/exec"

	"cloud.google.com/go/storage"
	vision "cloud.google.com/go/vision/apiv1"
	"cloud.google.com/go/vision/v2/apiv1/visionpb"
)

// Global API clients used across function invocations.
var (
	storageClient *storage.Client
	visionClient  *vision.ImageAnnotatorClient
)

func init() {
	// Declare a separate err variable to avoid shadowing the client variables.
	var err error

	storageClient, err = storage.NewClient(context.Background())
	if err != nil {
		log.Fatalf("storage.NewClient: %v", err)
	}

	visionClient, err = vision.NewImageAnnotatorClient(context.Background())
	if err != nil {
		log.Fatalf("vision.NewAnnotatorClient: %v", err)
	}
}

Java



import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.SafeSearchAnnotation;
import functions.eventpojos.GcsEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ImageMagick implements BackgroundFunction<GcsEvent> {

  private static Storage storage = StorageOptions.getDefaultInstance().getService();
  private static final String BLURRED_BUCKET_NAME = System.getenv("BLURRED_BUCKET_NAME");
  private static final Logger logger = Logger.getLogger(ImageMagick.class.getName());
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Google.Cloud.Storage.V1;
using Google.Cloud.Vision.V1;
using Google.Events.Protobuf.Cloud.Storage.V1;
using Grpc.Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace ImageMagick;

// Dependency injection configuration, executed during server startup.
public class Startup : FunctionsStartup
{
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
        services
            .AddSingleton(ImageAnnotatorClient.Create())
            .AddSingleton(StorageClient.Create());
}

[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<StorageObjectData>
{
    /// <summary>
    /// The bucket to store blurred images in. An alternative to using environment variables here would be to
    /// fetch it from IConfiguration.
    /// </summary>
    private static readonly string s_blurredBucketName = Environment.GetEnvironmentVariable("BLURRED_BUCKET_NAME");

    private readonly ImageAnnotatorClient _visionClient;
    private readonly StorageClient _storageClient;
    private readonly ILogger _logger;

    public Function(ImageAnnotatorClient visionClient, StorageClient storageClient, ILogger<Function> logger) =>
        (_visionClient, _storageClient, _logger) = (visionClient, storageClient, logger);

}

Ruby

require "functions_framework"

FunctionsFramework.on_startup do
  set_global :storage_client do
    require "google/cloud/storage"
    Google::Cloud::Storage.new
  end

  set_global :vision_client do
    require "google/cloud/vision"
    Google::Cloud::Vision.image_annotator
  end
end

PHP

use Google\CloudFunctions\CloudEvent;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Likelihood;
use Google\Rpc\Code;

이미지 분석

다음 함수는 이미지를 저장하기 위해 만든 Cloud Storage 버킷에 이미지가 업로드될 때 호출됩니다. 해당 함수는 Vision API를 사용하여 업로드된 이미지에서 폭력적인 콘텐츠 또는 성인 콘텐츠를 감지합니다.

Node.js

// Blurs uploaded images that are flagged as Adult or Violence.
exports.blurOffensiveImages = async event => {
  // This event represents the triggering Cloud Storage object.
  const object = event;

  const file = storage.bucket(object.bucket).file(object.name);
  const filePath = `gs://${object.bucket}/${object.name}`;

  console.log(`Analyzing ${file.name}.`);

  try {
    const [result] = await client.safeSearchDetection(filePath);
    const detections = result.safeSearchAnnotation || {};

    if (
      // Levels are defined in https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse#likelihood
      detections.adult === 'VERY_LIKELY' ||
      detections.violence === 'VERY_LIKELY'
    ) {
      console.log(`Detected ${file.name} as inappropriate.`);
      return await blurImage(file, BLURRED_BUCKET_NAME);
    } else {
      console.log(`Detected ${file.name} as OK.`);
    }
  } catch (err) {
    console.error(`Failed to analyze ${file.name}.`, err);
    throw err;
  }
};

Python

# Blurs uploaded images that are flagged as Adult or Violence.
def blur_offensive_images(data, context):
    file_data = data

    file_name = file_data["name"]
    bucket_name = file_data["bucket"]

    blob = storage_client.bucket(bucket_name).get_blob(file_name)
    blob_uri = f"gs://{bucket_name}/{file_name}"
    blob_source = vision.Image(source=vision.ImageSource(gcs_image_uri=blob_uri))

    # Ignore already-blurred files
    if file_name.startswith("blurred-"):
        print(f"The image {file_name} is already blurred.")
        return

    print(f"Analyzing {file_name}.")

    result = vision_client.safe_search_detection(image=blob_source)
    detected = result.safe_search_annotation

    # Process image
    if detected.adult == 5 or detected.violence == 5:
        print(f"The image {file_name} was detected as inappropriate.")
        return __blur_image(blob)
    else:
        print(f"The image {file_name} was detected as OK.")

Go


// GCSEvent is the payload of a GCS event.
type GCSEvent struct {
	Bucket string `json:"bucket"`
	Name   string `json:"name"`
}

// BlurOffensiveImages blurs offensive images uploaded to GCS.
func BlurOffensiveImages(ctx context.Context, e GCSEvent) error {
	outputBucket := os.Getenv("BLURRED_BUCKET_NAME")
	if outputBucket == "" {
		return errors.New("BLURRED_BUCKET_NAME must be set")
	}

	img := vision.NewImageFromURI(fmt.Sprintf("gs://%s/%s", e.Bucket, e.Name))

	resp, err := visionClient.DetectSafeSearch(ctx, img, nil)
	if err != nil {
		return fmt.Errorf("AnnotateImage: %w", err)
	}

	if resp.GetAdult() == visionpb.Likelihood_VERY_LIKELY ||
		resp.GetViolence() == visionpb.Likelihood_VERY_LIKELY {
		return blur(ctx, e.Bucket, outputBucket, e.Name)
	}
	log.Printf("The image %q was detected as OK.", e.Name)
	return nil
}

Java

@Override
// Blurs uploaded images that are flagged as Adult or Violence.
public void accept(GcsEvent event, Context context) {
  // Validate parameters
  if (event.getBucket() == null || event.getName() == null) {
    logger.severe("Error: Malformed GCS event.");
    return;
  }

  BlobInfo blobInfo = BlobInfo.newBuilder(event.getBucket(), event.getName()).build();

  // Construct URI to GCS bucket and file.
  String gcsPath = String.format("gs://%s/%s", event.getBucket(), event.getName());
  logger.info(String.format("Analyzing %s", event.getName()));

  // Construct request.
  ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
  List<AnnotateImageRequest> requests = List.of(request);

  // Send request to the Vision API.
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        logger.info(String.format("Error: %s", res.getError().getMessage()));
        return;
      }
      // Get Safe Search Annotations
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
        logger.info(String.format("Detected %s as inappropriate.", event.getName()));
        blur(blobInfo);
      } else {
        logger.info(String.format("Detected %s as OK.", event.getName()));
      }
    }
  } catch (IOException e) {
    logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
  }
}

C#

public async Task HandleAsync(CloudEvent cloudEvent, StorageObjectData data, CancellationToken cancellationToken)
{
    // Validate parameters
    if (data.Bucket is null || data.Name is null)
    {
        _logger.LogError("Malformed GCS event.");
        return;
    }

    // Construct URI to GCS bucket and file.
    string gcsUri = $"gs://{data.Bucket}/{data.Name}";
    _logger.LogInformation("Analyzing {uri}", gcsUri);

    // Perform safe search detection using the Vision API.
    Image image = Image.FromUri(gcsUri);
    SafeSearchAnnotation annotation;
    try
    {
        annotation = await _visionClient.DetectSafeSearchAsync(image);
    }
    // If the call to the Vision API fails, log the error but let the function complete normally.
    // If the exceptions weren't caught (and just propagated) the event would be retried.
    // See the "Best Practices" section in the documentation for more details about retry.
    catch (AnnotateImageException e)
    {
        _logger.LogError(e, "Vision API reported an error while performing safe search detection");
        return;
    }
    catch (RpcException e)
    {
        _logger.LogError(e, "Error communicating with the Vision API");
        return;
    }

    if (annotation.Adult == Likelihood.VeryLikely || annotation.Violence == Likelihood.VeryLikely)
    {
        _logger.LogInformation("Detected {uri} as inappropriate.", gcsUri);
        await BlurImageAsync(data, cancellationToken);
    }
    else
    {
        _logger.LogInformation("Detected {uri} as OK.", gcsUri);
    }
}

Ruby

# Blurs uploaded images that are flagged as Adult or Violence.
FunctionsFramework.cloud_event "blur_offensive_images" do |event|
  # Event-triggered Ruby functions receive a CloudEvents::Event::V1 object.
  # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html
  # The storage event payload can be obtained from the event data.
  payload = event.data
  file_name = payload["name"]
  bucket_name = payload["bucket"]

  # Ignore already-blurred files
  if file_name.start_with? "blurred-"
    logger.info "The image #{file_name} is already blurred."
    return
  end

  # Get image annotations from the Vision service
  logger.info "Analyzing #{file_name}."
  gs_uri = "gs://#{bucket_name}/#{file_name}"
  result = global(:vision_client).safe_search_detection image: gs_uri
  annotation = result.responses.first.safe_search_annotation

  # Respond to annotations by possibly blurring the image
  if annotation.adult == :VERY_LIKELY || annotation.violence == :VERY_LIKELY
    logger.info "The image #{file_name} was detected as inappropriate."
    blur_image bucket_name, file_name
  else
    logger.info "The image #{file_name} was detected as OK."
  end
end

PHP

function blurOffensiveImages(CloudEvent $cloudevent): void
{
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');

    $storage = new StorageClient();
    $data = $cloudevent->getData();

    $file = $storage->bucket($data['bucket'])->object($data['name']);
    $filePath = 'gs://' . $data['bucket'] . '/' . $data['name'];
    fwrite($log, 'Analyzing ' . $filePath . PHP_EOL);

    $annotator = new ImageAnnotatorClient();
    $storage = new StorageClient();

    try {
        $response = $annotator->safeSearchDetection($filePath);

        // Handle error
        if ($response->hasError()) {
            $code = Code::name($response->getError()->getCode());
            $message = $response->getError()->getMessage();
            fwrite($log, sprintf('%s: %s' . PHP_EOL, $code, $message));
            return;
        }

        $annotation = $response->getSafeSearchAnnotation();

        $isInappropriate =
            $annotation->getAdult() === Likelihood::VERY_LIKELY ||
            $annotation->getViolence() === Likelihood::VERY_LIKELY;

        if ($isInappropriate) {
            fwrite($log, 'Detected ' . $data['name'] . ' as inappropriate.' . PHP_EOL);
            $blurredBucketName = getenv('BLURRED_BUCKET_NAME');

            blurImage($log, $file, $blurredBucketName);
        } else {
            fwrite($log, 'Detected ' . $data['name'] . ' as OK.' . PHP_EOL);
        }
    } catch (Exception $e) {
        fwrite($log, 'Failed to analyze ' . $data['name'] . PHP_EOL);
        fwrite($log, $e->getMessage() . PHP_EOL);
    }
}

이미지 흐리게 처리하기

업로드된 이미지에서 폭력적인 콘텐츠나 성인 콘텐츠가 감지되면 다음 함수가 호출됩니다. 해당 함수는 불쾌감을 주는 이미지를 다운로드하고 ImageMagick을 사용하여 이미지를 흐리게 처리한 다음, 해당 이미지를 업로드하여 원본 이미지를 덮어씁니다.

Node.js

// Blurs the given file using ImageMagick, and uploads it to another bucket.
const blurImage = async (file, blurredBucketName) => {
  const tempLocalPath = `/tmp/${path.parse(file.name).base}`;

  // Download file from bucket.
  try {
    await file.download({destination: tempLocalPath});

    console.log(`Downloaded ${file.name} to ${tempLocalPath}.`);
  } catch (err) {
    throw new Error(`File download failed: ${err}`);
  }

  await new Promise((resolve, reject) => {
    gm(tempLocalPath)
      .blur(0, 16)
      .write(tempLocalPath, (err, stdout) => {
        if (err) {
          console.error('Failed to blur image.', err);
          reject(err);
        } else {
          console.log(`Blurred image: ${file.name}`);
          resolve(stdout);
        }
      });
  });

  // Upload result to a different bucket, to avoid re-triggering this function.
  const blurredBucket = storage.bucket(blurredBucketName);

  // Upload the Blurred image back into the bucket.
  const gcsPath = `gs://${blurredBucketName}/${file.name}`;
  try {
    await blurredBucket.upload(tempLocalPath, {destination: file.name});
    console.log(`Uploaded blurred image to: ${gcsPath}`);
  } catch (err) {
    throw new Error(`Unable to upload blurred image to ${gcsPath}: ${err}`);
  }

  // Delete the temporary file.
  return fs.unlink(tempLocalPath);
};

Python

# Blurs the given file using ImageMagick.
def __blur_image(current_blob):
    file_name = current_blob.name
    _, temp_local_filename = tempfile.mkstemp()

    # Download file from bucket.
    current_blob.download_to_filename(temp_local_filename)
    print(f"Image {file_name} was downloaded to {temp_local_filename}.")

    # Blur the image using ImageMagick.
    with Image(filename=temp_local_filename) as image:
        image.resize(*image.size, blur=16, filter="hamming")
        image.save(filename=temp_local_filename)

    print(f"Image {file_name} was blurred.")

    # Upload result to a second bucket, to avoid re-triggering the function.
    # You could instead re-upload it to the same bucket + tell your function
    # to ignore files marked as blurred (e.g. those with a "blurred" prefix)
    blur_bucket_name = os.getenv("BLURRED_BUCKET_NAME")
    blur_bucket = storage_client.bucket(blur_bucket_name)
    new_blob = blur_bucket.blob(file_name)
    new_blob.upload_from_filename(temp_local_filename)
    print(f"Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}")

    # Delete the temporary file.
    os.remove(temp_local_filename)

Go


// blur blurs the image stored at gs://inputBucket/name and stores the result in
// gs://outputBucket/name.
func blur(ctx context.Context, inputBucket, outputBucket, name string) error {
	inputBlob := storageClient.Bucket(inputBucket).Object(name)
	r, err := inputBlob.NewReader(ctx)
	if err != nil {
		return fmt.Errorf("NewReader: %w", err)
	}

	outputBlob := storageClient.Bucket(outputBucket).Object(name)
	w := outputBlob.NewWriter(ctx)
	defer w.Close()

	// Use - as input and output to use stdin and stdout.
	cmd := exec.Command("convert", "-", "-blur", "0x8", "-")
	cmd.Stdin = r
	cmd.Stdout = w

	if err := cmd.Run(); err != nil {
		return fmt.Errorf("cmd.Run: %w", err)
	}

	log.Printf("Blurred image uploaded to gs://%s/%s", outputBlob.BucketName(), outputBlob.ObjectName())

	return nil
}

Java

// Blurs the file described by blobInfo using ImageMagick,
// and uploads it to the blurred bucket.
private static void blur(BlobInfo blobInfo) throws IOException {
  String bucketName = blobInfo.getBucket();
  String fileName = blobInfo.getName();

  // Download image
  Blob blob = storage.get(BlobId.of(bucketName, fileName));
  Path download = Paths.get("/tmp/", fileName);
  blob.downloadTo(download);

  // Construct the command.
  Path upload = Paths.get("/tmp/", "blurred-" + fileName);
  List<String> args = List.of("convert", download.toString(), "-blur", "0x8", upload.toString());
  try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process process = pb.start();
    process.waitFor();
  } catch (Exception e) {
    logger.info(String.format("Error: %s", e.getMessage()));
  }

  // Upload image to blurred bucket.
  BlobId blurredBlobId = BlobId.of(BLURRED_BUCKET_NAME, fileName);
  BlobInfo blurredBlobInfo =
      BlobInfo.newBuilder(blurredBlobId).setContentType(blob.getContentType()).build();

  byte[] blurredFile = Files.readAllBytes(upload);
  storage.create(blurredBlobInfo, blurredFile);
  logger.info(
      String.format("Blurred image uploaded to: gs://%s/%s", BLURRED_BUCKET_NAME, fileName));

  // Remove images from fileSystem
  Files.delete(download);
  Files.delete(upload);
}

C#

/// <summary>
/// Downloads the Storage object specified by <paramref name="data"/>,
/// blurs it using ImageMagick, and uploads it to the "blurred" bucket.
/// </summary>
private async Task BlurImageAsync(StorageObjectData data, CancellationToken cancellationToken)
{
    // Download image
    string originalImageFile = Path.GetTempFileName();
    using (Stream output = File.Create(originalImageFile))
    {
        await _storageClient.DownloadObjectAsync(data.Bucket, data.Name, output, cancellationToken: cancellationToken);
    }

    // Construct the ImageMagick command
    string blurredImageFile = Path.GetTempFileName();
    // Command-line arguments for ImageMagick.
    // Paths are wrapped in quotes in case they contain spaces.
    string arguments = $"\"{originalImageFile}\" -blur 0x8, \"{blurredImageFile}\"";

    // Run the ImageMagick command line tool ("convert").
    Process process = Process.Start("convert", arguments);
    // Process doesn't expose a way of asynchronously waiting for completion.
    // See https://stackoverflow.com/questions/470256 for examples of how
    // this can be achieved using events, but for the sake of brevity,
    // this sample just waits synchronously.
    process.WaitForExit();

    // If ImageMagick failed, log the error but complete normally to avoid retrying.
    if (process.ExitCode != 0)
    {
        _logger.LogError("ImageMagick exited with code {exitCode}", process.ExitCode);
        return;
    }

    // Upload image to blurred bucket.
    using (Stream input = File.OpenRead(blurredImageFile))
    {
        await _storageClient.UploadObjectAsync(
            s_blurredBucketName, data.Name, data.ContentType, input, cancellationToken: cancellationToken);
    }

    string uri = $"gs://{s_blurredBucketName}/{data.Name}";
    _logger.LogInformation("Blurred image uploaded to: {uri}", uri);

    // Remove images from the file system.
    File.Delete(originalImageFile);
    File.Delete(blurredImageFile);
}

Ruby

require "tempfile"
require "mini_magick"

# Blurs the given file using ImageMagick.
def blur_image bucket_name, file_name
  tempfile = Tempfile.new
  begin
    # Download the image file
    bucket = global(:storage_client).bucket bucket_name
    file = bucket.file file_name
    file.download tempfile
    tempfile.close

    # Blur the image using ImageMagick
    MiniMagick::Image.new tempfile.path do |image|
      image.blur "0x16"
    end
    logger.info "Image #{file_name} was blurred"

    # Upload result to a second bucket, to avoid re-triggering the function.
    # You could instead re-upload it to the same bucket and tell your function
    # to ignore files marked as blurred (e.g. those with a "blurred" prefix.)
    blur_bucket_name = ENV["BLURRED_BUCKET_NAME"]
    blur_bucket = global(:storage_client).bucket blur_bucket_name
    blur_bucket.create_file tempfile.path, file_name
    logger.info "Blurred image uploaded to gs://#{blur_bucket_name}/#{file_name}"
  ensure
    # Ruby will remove the temp file when garbage collecting the object,
    # but it is good practice to remove it explicitly.
    tempfile.unlink
  end
end

PHP

// Blurs the given file using ImageMagick, and uploads it to another bucket.
function blurImage(
    $log,
    Object $file,
    string $blurredBucketName
): void {
    $tempLocalPath = sys_get_temp_dir() . '/' . $file->name();

    // Download file from bucket.
    $image = new Imagick();
    try {
        $image->readImageBlob($file->downloadAsStream());
    } catch (Exception $e) {
        throw new Exception('Streaming download failed: ' . $e);
    }

    // Blur file using ImageMagick
    // (The Imagick class is from the PECL 'imagick' package)
    $image->blurImage(0, 16);

    // Stream blurred image result to a different bucket. // (This avoids re-triggering this function.)
    $storage = new StorageClient();
    $blurredBucket = $storage->bucket($blurredBucketName);

    // Upload the Blurred image back into the bucket.
    $gcsPath = 'gs://' . $blurredBucketName . '/' . $file->name();
    try {
        $blurredBucket->upload($image->getImageBlob(), [
            'name' => $file->name()
        ]);
        fwrite($log, 'Streamed blurred image to: ' . $gcsPath . PHP_EOL);
    } catch (Exception $e) {
        throw new Exception(
            sprintf(
                'Unable to stream blurred image to %s: %s',
                $gcsPath,
                $e->getMessage()
            )
        );
    }
}

함수 배포

스토리지 트리거를 사용하여 함수를 배포하려면 샘플 코드가 포함된 디렉터리(또는 Java의 경우 pom.xml 파일)에서 다음 명령어를 실행합니다.

Node.js

gcloud functions deploy blurOffensiveImages \
--no-gen2 \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

Python

gcloud functions deploy blur_offensive_images \
--no-gen2 \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

Go

gcloud functions deploy BlurOffensiveImages \
--no-gen2 \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

Java

gcloud functions deploy java-blur-function \
--no-gen2 \
--entry-point=functions.ImageMagick \
--runtime=RUNTIME \
--memory 512MB \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

C#

gcloud functions deploy csharp-blur-function \
--no-gen2 \
--entry-point=ImageMagick.Function \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

Ruby

gcloud functions deploy blur_offensive_images \
--no-gen2 \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

PHP

gcloud functions deploy blurOffensiveImages \
--no-gen2 \
--runtime=RUNTIME \
--trigger-bucket=YOUR_INPUT_BUCKET_NAME \
--set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

다음을 바꿉니다.

  • RUNTIME: Ubuntu 18.04를 기반으로 하는 런타임(이후 런타임에는 ImageMagick에 대한 지원이 포함되지 않음)
  • YOUR_INPUT_BUCKET_NAME: 이미지를 업로드할 Cloud Storage 버킷의 이름
  • YOUR_OUTPUT_BUCKET_NAME: 흐리게 처리된 이미지를 저장할 버킷의 이름

이 특정 예시에서는 deploy 명령어에 버킷 이름의 일부로 gs://를 포함하지 마세요.

이미지 업로드

  1. 살점을 뜯어먹는 좀비와 같은 불쾌감을 주는 이미지를 업로드합니다.

    gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME

    여기서, YOUR_INPUT_BUCKET_NAME은 이미지를 업로드하기 위해 앞서 만든 Cloud Storage 버킷입니다.

  2. 로그를 확인하여 실행이 완료되었는지 확인합니다.

    gcloud functions logs read --limit 100
  3. 앞서 만든 YOUR_OUTPUT_BUCKET_NAME Cloud Storage 버킷에서 흐리게 처리된 이미지를 확인할 수 있습니다.

삭제

이 튜토리얼에서 사용된 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하세요.

프로젝트 삭제

비용이 청구되지 않도록 하는 가장 쉬운 방법은 튜토리얼에서 만든 프로젝트를 삭제하는 것입니다.

프로젝트를 삭제하는 방법은 다음과 같습니다.

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

함수 삭제

Cloud Run Functions를 삭제해도 Cloud Storage에 저장된 리소스는 삭제되지 않습니다.

이 튜토리얼에서 배포한 함수를 삭제하려면 다음 명령어를 실행합니다.

Node.js

gcloud functions delete blurOffensiveImages 

Python

gcloud functions delete blur_offensive_images 

Go

gcloud functions delete BlurOffensiveImages 

Java

gcloud functions delete java-blur-function 

C#

gcloud functions delete csharp-blur-function 

Ruby

gcloud functions delete blur_offensive_images 

PHP

gcloud functions delete blurOffensiveImages 

Google Cloud 콘솔에서 Cloud Run Functions를 삭제할 수도 있습니다.