반응형
Object Storage의 경우 Upload, Delete 등 특정 이벤트가 발생 했을때 알람을 발생시켜주는 기능을 직접적으로 제공하지는 않고, Cloud Functions 서비스와 연계하여 사용해야 합니다.
이번 포스팅은 Object Storage에 이벤트가 발생할때마다 메일을 통해 알람을 발송시키는 방법에 대한 글입니다.
1. 버킷우측 버튼을 클릭하여 "이벤트 관리" 메뉴를 선택합니다.
2. 우측 생성버튼을 클릭합니다.
3. 이벤트 유형을 선택하고, "트리거 생성" 버튼을 클릭하여 트리거를 생성 합니다.
* 필터의 경우 선택하지 않으면 모든 오브젝트에 대해 이벤트 알람이 발생합니다.
4. 우측 하단의 "신규 액션 생성" 버튼을 클릭하여 신규 액션을 생성합니다.
5. 이벤트 발생시 Cloud Outbound Mailer 서비스를 통해 사용자에게 알람을 보내는 Python API코드를 작성합니다.
Accesskey와 SecretKey를 입력하고, body.recipients.address 부분에 알람을 전달받을 메일 주소를 입력합니다.
def main(args):
import hashlib
import hmac
import base64
import time
import requests
def make_signature(access_key, secret_key, method, uri, timestamp):
timestamp = str(timestamp)
secret_key = bytes(secret_key, 'UTF-8')
message = method + " " + uri + "\n" + timestamp + "\n" + access_key
message = bytes(message, 'UTF-8')
signingKey = base64.b64encode(hmac.new(secret_key, message, digestmod=hashlib.sha256).digest()).decode()
return signingKey
def send_request(url, headers, method, data):
r = requests.request(method, url, headers=headers, json=data)
if r.status_code != 200:
print('ERROR! {} {}'.format(r.status_code, r.text))
return r.text
def send_mail(bucket_name, object_name, t):
timestamp = int(time.time()*1000)
access_key = ""
secret_key = ""
host = 'https://mail.apigw.ntruss.com'
path = '/api/v1/mails'
method = 'POST'
headers = {
"Content-Type": "application/json",
"x-ncp-apigw-timestamp": str(timestamp),
"x-ncp-iam-access-key": access_key,
"x-ncp-apigw-signature-v2": make_signature(access_key, secret_key, method, path, timestamp)
}
body = {
"senderAddress": "no_reply@company.com",
"title": "alert: ${object_name}",
"body": 'bucket: ${bucket_name} / event_type: ${event_type} / path: ${object_name} / time: ${time}',
"recipients": [
{
"address": "sungjun7305@gmail.com",
"name": "Hong Seongjoon",
"type": "R",
"parameters": {
"bucket_name": bucket_name,
"event_type": event_type,
"object_name": object_name,
"time": t
}
}
],
"individual": True,
"advertising": False
}
send_request(host+path, headers, method, body)
print('data: {}'.format(str(args)))
bucket_name = args.get("container_name", "")
object_name = args.get("object_name", "")
t = args.get("timestamp_finish", "")
event_type = args.get("event_type", "")
send_mail(bucket_name, object_name, t)
return args
이외 다른 인자들입니다.
{
"container_name": "버킷 이름",
"event_name": "이벤트 이름",
"event_type": "이벤트 종류",
"event_version": "1.0",
"object_length": "객체 크기",
"object_name": "객체 키",
"region": "리전 이름",
"remote_address": "요청 IP",
"remote_user_sha256": "사용자 Access Key의 SHA256 hash hex값",
"request_method": "요청 Method",
"remote_user_type": "사용자 종류",
"request_type": "요청 종류",
"timestamp_finish": "요청 처리가 끝난 시간, 유닉스 시간, 밀리초",
"timestamp_start": "요청 처리를 시작한 시간, 유닉스 시간, 밀리초"
}
6. 액션을 생성 했으면 다시 트리거생성 창에서 신규생성한 액션을 선택하여 트리거를 생성 합니다.
7. 생성한 트리거를 선택하고 버킷 이벤트를 생성 합니다.
8. 이벤트가 정상적으로 생성 되었습니다.
9. 테스트로 아무 파일을 선택하여 업로드 합니다.
10. 메일함을 확인해보면 정상적으로 알람을 전달 받은것을 확인할 수 있습니다.
Object Storage서비스와 Cloud Function, Cloud Outbound Mailer 서비스를 활용하여 Object Storage서비스의 이벤트유형에 따른 알람을 발생시키도록 해보았습니다.
반응형
'Public Cloud > NCP' 카테고리의 다른 글
[NKS]Ncloud Kubernetes Service 구축 방법 (1) | 2023.08.07 |
---|