Skip To Content

示例:注册文本文件中列出的文件夹和数据库

此脚本示例使用要注册到 ArcGIS Server 站点的文件夹和数据库的文本文件列表。 虽然您可以使用 ArcGIS ProArcGIS Enterprise 门户将数据库和文件夹注册到联合 ArcGIS Server 站点,并使用 ArcGIS ProArcGIS Server Manager 将数据库和文件夹注册到独立 ArcGIS Server 站点,但如果您有大量需要注册的数据源位置,则使用此脚本可能会更快。

提示:

有关为何将数据位置注册到 ArcGIS Server 站点的信息,请参阅 ArcGIS Server 的数据源

设置输入文本文件的结构,具体如下:

#dsName|dsType|serverPath|clientPath|hostname

dsName=<data store name>|dsType={FOLDER | DATABASE}|serverPath=<path to folder or database connection file used by the ArcGIS Server site>|clientPath=<path to folder or database connection file used by the publishing client>

  • dsName - 数据存储项目的名称。 该名称可以是您提供的任何名称。
  • dsType - 要注册的数据存储项目的类型。 在此示例中,可以使用 FOLDERDATABASE
  • serverPath - 服务器计算机查看数据的路径。
  • clientPath - 发布者计算机查看数据的路径。 如果未提供该路径,则将使用服务器路径。 如果您提供关键字 managed,则表示要将此数据存储项目注册为 ArcGIS Server 站点管理的数据库。
  • hostname - 发布者计算机的名称。 仅在 clientPath 为基于文件夹的路径时需要此名称。 如果忽略该属性,则将使用正在运行脚本的计算机的名称。

在此示例中,文本文件中列出了两个文件夹路径和两个数据库连接文件。 第二个文件夹路径 (testData) 具有不同的 ArcGIS Server 站点和本地 ArcGIS 客户端位置。

#dsName|dsType|serverPath|clientPath|hostname

dsName=sharedData|dsType=FOLDER|serverPath=\\sharedserver\data|clientPath=\\sharedserver\data
dsName=testData|dsType=FOLDER|serverPath=\\test_server\data|clientPath=c:\mydata|hostname=qalab_publisher
dsName=entGeodatabase|dsType=DATABASE|serverPath=C:\data\db_connections\Connection to enterprisegdb1.sde|clientPath=C:\data\db_Connections\Connection to enterprisegdb1.sde
dsName=spatialDatabase|dsType=DATABASE|serverPath=C:\data\db_Connections\Connection to projectdb.sde|clientPath=C:\data\db_Connections\Connection to projectdb.sde

以下示例脚本将提示您输入 ArcGIS Server 站点的连接信息以创建 ArcGIS Server 连接文件。 该脚本还将提示您指定包含数据源列表的文本文件的位置,并将文件中的数据源注册到 ArcGIS Server 站点。

import sys
import os
import getpass
import arcpy
import codecs


def main(argv=None):
    
    # Ask for admin user name and password
    
    username = input("Enter user name: ")
    password = getpass.getpass("Enter password: ")
    
    # Ask for server name & port
    serverName = input("Enter server name: ")
    serverPort = input("Enter server port: ")
 
    # Create a connection file to the server and save it in the same location as that of the script
    serverURL="http://"+serverName+":"+str(serverPort)+"/arcgis/admin"
    try:
        arcpy.mapping.CreateGISServerConnectionFile("PUBLISH_GIS_SERVICES",os.curdir,serverName+".ags",serverURL,"ARCGIS_SERVER",username=username,password=password)
    except Exception as e:
        print(e)
    
    agsConnection = os.path.join(os.curdir, serverName+".ags")
    
    if not os.path.isfile(agsConnection):
        print("Unable to connect to ArcGIS Server. Exiting.")
        sys.exit(1)

    # Input File that contains the data store information
    dataStoresFile = input("Path to pipe-delimited text file containing datastore information: ")
    
    num = 0 
    datastores = {}
    
    for datastoreRow in readlinesFromInputFile(dataStoresFile):
            
        datastoreEntry = {}
        
        for index in range(len(datastoreRow)):
            
            datastoreProp = datastoreRow[index].split("=")
            
            if datastoreProp[0] == "dsName":
                datastoreEntry["connection_name"] = datastoreProp[1]
            if datastoreProp[0] == "dsType":
                datastoreEntry["datastore_type"] = datastoreProp[1]
            if datastoreProp[0] == "serverPath":
                datastoreEntry["server_path"] = datastoreProp[1]
            if datastoreProp[0] == "clientPath":
                datastoreEntry["client_path"] = datastoreProp[1]
            if datastoreProp[0] == "hostname":
                datastoreEntry["hostname"] = datastoreProp[1]
         
            # Add the datastore information to a dictionary
            datastores["datastore" + str(num)] = datastoreEntry
        
        num +=1

    # Call helper functions to register datastores
    addDataStores(datastores,agsConnection)


# A function that reads lines from the input file
def readlinesFromInputFile(filename, delim='|'):
    file = codecs.open(filename,'r','utf-8-sig')
    for line in file.readlines():
        # Remove the trailing whitespaces and the newline characters
        line = line.rstrip()
        
        if line.startswith('#') or len(line) == 0:
            pass # Skip the lines that contain # at the beginning or any empty lines
        else:
            # Split the current line into list
            yield line.split(delim)
    file.close()


def addDataStores(datastoresDict,agsConnection):
    
    for datastoreToAdd in datastoresDict:
        # Build the dictionary with the role name and description               
        datastoresDict[datastoreToAdd]["connection_file"] =  agsConnection
        
        print("Adding the datastore: " + datastoresDict[datastoreToAdd]['connection_name'])
        
        try:
            arcpy.AddDataStoreItem(**datastoresDict[datastoreToAdd])
            print("Successfully added the datastore: " + datastoresDict[datastoreToAdd]['connection_name'])
        except Exception as e:
            print("Adding of the datastore: " + datastoresDict[datastoreToAdd]['connection_name'] + " failed.")
            print(e)

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))