代码示例UploadServiceDefinition 示例 1(独立脚本)
以下脚本将地图中的图层列表作为 web 切片图层发布至 ArcGIS Enterprise 或 ArcGIS Online。门户信息可从 ArcGIS Pro 的门户页面中获得。
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service = "TileSharingDraftExample"
sddraft_filename = service + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps("World")[0]
lyrs = []
lyrs.append(m.listLayers('cities')[0])
lyrs.append(m.listLayers('counties')[0])
# Create TileSharingDraft and set service properties
sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "TILE", service, lyrs)
sharing_draft.summary = "My Summary"
sharing_draft.tags = "My Tags"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"
# Create Service Definition Draft file
sharing_draft.exportToSDDraft(sddraft_output_filename)
# Stage Service
sd_filename = service + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
# Share to portal
print("Uploading Service Definition...")
arcpy.UploadServiceDefinition_server(sd_output_filename, "My Hosted Services")
print("Successfully Uploaded service.")UploadServiceDefinition 示例 2(独立脚本)
以下脚本可上传现有的服务定义并可使用可选参数对该服务的属性进行修改。
import arcpy
# Set environment settings
arcpy.env.workspace = "C:/data"
# Set local variables
inSdFile = "myMapService.sd"
inServer = "myServerConnection.ags"
inServiceName = "newServiceName"
inCluster = "myCluster"
inFolderType = "NEW"
inFolder = "newFolder"
inStartup = "STOPPED"
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(inSdFile, inServer, inServiceName,
inCluster, inFolderType, inFolder,
inStartup)UploadServiceDefinition 示例 3(独立脚本)
以下脚本可将现有的服务定义上传到 ArcGIS Online 或 ArcGIS Enterprise 并可使用可选参数对该服务的属性进行修改。
import arcpy
# Set environment settings arcpy.env.workspace = "C:/data"
# Set local variables inSdFile = "myMapService.sd"
inServer = "HOSTING_SERVER"
inServiceName = "newServiceName"
inCluster = "myCluster"
inFolderType = "EXISTING"
inFolder = "existingFolder"
inStartup = "STOPPED"
inOverride = "OVERRIDE_DEFINITION"
inMyContents = "SHARE_ONLINE"
inPublic = "PRIVATE"
inOrganization = "NO_SHARE_ORGANIZATION"
inGroups = ["My Group", "MyGroup 2"]
# Execute UploadServiceDefinition arcpy.UploadServiceDefinition_server(inSdFile, inServer, inServiceName, inCluster, inFolderType, inFolder,
inStartup, inOverride, inMyContents, inPublic, inOrganization, inGroups)