Cloud Pub/Sub 푸시 구독으로 전달된 메시지를 처리하는 서비스입니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
C#
app.MapPost("/", (Envelope envelope) =>
{
if (envelope?.Message?.Data == null)
{
app.Logger.LogWarning("Bad Request: Invalid Pub/Sub message format.");
return Results.BadRequest();
}
var data = Convert.FromBase64String(envelope.Message.Data);
var target = System.Text.Encoding.UTF8.GetString(data);
app.Logger.LogInformation($"Hello {target}!");
return Results.NoContent();
});
Go
// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
Message struct {
Data []byte `json:"data,omitempty"`
ID string `json:"id"`
} `json:"message"`
Subscription string `json:"subscription"`
}
// HelloPubSub receives and processes a Pub/Sub push message.
func HelloPubSub(w http.ResponseWriter, r *http.Request) {
var m PubSubMessage
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ioutil.ReadAll: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// byte slice unmarshalling handles base64 decoding.
if err := json.Unmarshal(body, &m); err != nil {
log.Printf("json.Unmarshal: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
name := string(m.Message.Data)
if name == "" {
name = "World"
}
log.Printf("Hello %s!", name)
}
Java
import com.example.cloudrun.Body;
import java.util.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
// PubsubController consumes a Pub/Sub message.
@RestController
public class PubSubController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity receiveMessage(@RequestBody Body body) {
// Get PubSub message from request body.
Body.Message message = body.getMessage();
if (message == null) {
String msg = "Bad Request: invalid Pub/Sub message format";
System.out.println(msg);
return new ResponseEntity(msg, HttpStatus.BAD_REQUEST);
}
String data = message.getData();
String target =
!StringUtils.isEmpty(data) ? new String(Base64.getDecoder().decode(data)) : "World";
String msg = "Hello " + target + "!";
System.out.println(msg);
return new ResponseEntity(msg, HttpStatus.OK);
}
}
Node.js
app.post('/', (req, res) => {
if (!req.body) {
const msg = 'no Pub/Sub message received';
console.error(`error: ${msg}`);
res.status(400).send(`Bad Request: ${msg}`);
return;
}
if (!req.body.message) {
const msg = 'invalid Pub/Sub message format';
console.error(`error: ${msg}`);
res.status(400).send(`Bad Request: ${msg}`);
return;
}
const pubSubMessage = req.body.message;
const name = pubSubMessage.data
? Buffer.from(pubSubMessage.data, 'base64').toString().trim()
: 'World';
console.log(`Hello ${name}!`);
res.status(204).send();
});
Python
@app.route("/", methods=["POST"])
def index():
envelope = request.get_json()
if not envelope:
msg = "no Pub/Sub message received"
print(f"error: {msg}")
return f"Bad Request: {msg}", 400
if not isinstance(envelope, dict) or "message" not in envelope:
msg = "invalid Pub/Sub message format"
print(f"error: {msg}")
return f"Bad Request: {msg}", 400
pubsub_message = envelope["message"]
name = "World"
if isinstance(pubsub_message, dict) and "data" in pubsub_message:
name = base64.b64decode(pubsub_message["data"]).decode("utf-8").strip()
print(f"Hello {name}!")
return ("", 204)
다음 단계
다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.