DeleteSchedule 搭配 a AWS SDK 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DeleteSchedule 搭配 a AWS SDK 使用

下列程式碼範例示範如何使用 DeleteSchedule

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
AWS SDK for .NET
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// <summary> /// Deletes an existing schedule from Amazon EventBridge Scheduler. /// </summary> /// <param name="name">The name of the schedule to delete.</param> /// <param name="groupName">The group name of the schedule to delete.</param> /// <returns>True if the schedule was deleted successfully, false otherwise.</returns> public async Task<bool> DeleteScheduleAsync(string name, string groupName) { try { var request = new DeleteScheduleRequest { Name = name, GroupName = groupName }; await _amazonScheduler.DeleteScheduleAsync(request); Console.WriteLine($"Successfully deleted schedule with name '{name}'."); return true; } catch (ResourceNotFoundException ex) { _logger.LogError( $"Failed to delete schedule with ID '{name}' because the resource was not found: {ex.Message}"); return true; } catch (Exception ex) { _logger.LogError( $"An error occurred while deleting schedule with ID '{name}': {ex.Message}"); return false; } }
  • 如需 API 詳細資訊,請參閱 DeleteSchedule AWS SDK for .NET 參考中的 API

Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a schedule with the specified name and group name. * * @param name the name of the schedule to be deleted * @param groupName the group name of the schedule to be deleted * @return a {@link CompletableFuture} that, when completed, indicates whether the schedule was successfully deleted * @throws CompletionException if an error occurs while deleting the schedule, except for the case where the schedule is not found */ public CompletableFuture<Boolean> deleteScheduleAsync(String name, String groupName) { DeleteScheduleRequest request = DeleteScheduleRequest.builder() .name(name) .groupName(groupName) .build(); CompletableFuture<DeleteScheduleResponse> response = getAsyncClient().deleteSchedule(request); return response.handle((result, ex) -> { if (ex != null) { if (ex instanceof ResourceNotFoundException) { throw new CompletionException("Resource not found while deleting schedule with ID: " + name, ex); } else { throw new CompletionException("Failed to delete schedule.", ex); } } logger.info("Successfully deleted schedule with name {}.", name); return true; }); }
  • 如需 API 詳細資訊,請參閱 DeleteSchedule AWS SDK for Java 2.x 參考中的 API

Python
Python 的 SDK (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class SchedulerWrapper: def __init__(self, eventbridge_scheduler_client: client): self.scheduler_client = eventbridge_scheduler_client @classmethod def from_client(cls) -> "SchedulerWrapper": """ Creates a SchedulerWrapper instance with a default EventBridge Scheduler client. :return: An instance of SchedulerWrapper initialized with the default EventBridge Scheduler client. """ eventbridge_scheduler_client = boto3.client("scheduler") return cls(eventbridge_scheduler_client) def delete_schedule(self, name: str, schedule_group_name: str) -> None: """ Deletes the schedule with the specified name and schedule group. :param name: The name of the schedule. :param schedule_group_name: The name of the schedule group. """ try: self.scheduler_client.delete_schedule( Name=name, GroupName=schedule_group_name ) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error( "Failed to delete schedule with ID '%s' because the resource was not found: %s", name, err.response["Error"]["Message"], ) else: logger.error( "Error deleting schedule: %s", err.response["Error"]["Message"] ) raise
  • 如需 API 詳細資訊,請參閱 DeleteSchedule AWS SDK for Python (Boto3) Word 參考中的 API